te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - Time from UTC + IANA time zone location - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Time from UTC + IANA time zone location - Stack Overflow

programmeradmin3浏览0评论

I see an excellent summary of handling time in webapp here. But, it doesn't clearly address the below scenario. I wanted a way to get the local time in web application based on date time at UTC + Location(iana timezone location - America/New_York).

I am looking for a library which does the following calculation. If it works natively in Javascript, that also suit my purpose.

From server, following information is retrieved

{
  dateTime: "2002-10-27T15:04:05Z" # Time in UTC, no TimeZone info
  userTimeZone: "America/New_York" # based on current user location
}

The issue is that, I couldn't find a way to convert it to the time at location (New York) considering daylight savings offset.

I am not sure saving time zone offset in database solve the issue as my user may be from across region and may look at the same data. For example, the event happened at a location belongs to Estern zone (EST/EDT), but the user at Pacific zone may query the data based on their location (8AM - 5PM "America/Los_Angeles" - PST/PDT).

I looked at Moment.js, but couldn't find a solution.

To summarize, I need a way to get the local time at a specific location(considering daylight savings offset) in web browser, from an input of date time at UTC + IANA location.

I see an excellent summary of handling time in webapp here. But, it doesn't clearly address the below scenario. I wanted a way to get the local time in web application based on date time at UTC + Location(iana timezone location - America/New_York).

I am looking for a library which does the following calculation. If it works natively in Javascript, that also suit my purpose.

From server, following information is retrieved

{
  dateTime: "2002-10-27T15:04:05Z" # Time in UTC, no TimeZone info
  userTimeZone: "America/New_York" # based on current user location
}

The issue is that, I couldn't find a way to convert it to the time at location (New York) considering daylight savings offset.

I am not sure saving time zone offset in database solve the issue as my user may be from across region and may look at the same data. For example, the event happened at a location belongs to Estern zone (EST/EDT), but the user at Pacific zone may query the data based on their location (8AM - 5PM "America/Los_Angeles" - PST/PDT).

I looked at Moment.js, but couldn't find a solution.

To summarize, I need a way to get the local time at a specific location(considering daylight savings offset) in web browser, from an input of date time at UTC + IANA location.

Share Improve this question edited May 23, 2017 at 11:51 CommunityBot 11 silver badge asked Jan 23, 2013 at 16:36 bsrbsr 58.7k88 gold badges217 silver badges321 bronze badges 1
  • Hi, what did you choose at the end? – Maziyar Commented Oct 14, 2013 at 5:53
Add a ment  | 

3 Answers 3

Reset to default 5

You can achieve this without a library using the toLocaleDateString method:

// prints 'Sun, Oct 27, 2002, 10:04 AM EST'
new Date('2002-10-27T15:04:05Z')
.toLocaleDateString('en-US', {
  timeZone:'America/New_York',
  timeZoneName: 'short',
  minute: 'numeric',
  hour: 'numeric',
  weekday: 'short',
  day: 'numeric',
  year: 'numeric',
  month: 'short',
});

Intl.DateTimeFormat works in a very similar fashion.

Tested on Chrome, Firefox and Safari (OSX). Note that according to MDN, the timezone property only needs to support 'UTC' and the full IANA database may not be implemented in all browsers. Feel free to update this answer with support across other setups.

Original Answer

MomentJS is an excellent library, but it is focused on parsing. You need TimezoneJS, which implements the Olson database in javascript.

Your use case is one of the first described in the documentation.

Updated Answer

There are multiple libraries for doing time zone conversions in JavaScript, as listed here. At the time this question was originally asked, I was only aware of TimezoneJS.

Since you asked about moment.js, you should use the moment-timezone plugin. (This wasn't available when you originally asked the question.)

var x = // your object as written in the question

var m = moment(x.dateTime).tz(x.userTimeZone);

var s = m.format(); // or whatever you want to do

The fine folks at MomentJS have created another library called Moment Timezone. I think this is exactly what you're looking for. Specifically:

var zone = moment.tz.zone('America/New_York');
zone.parse(Date.UTC(2012, 2, 19, 8, 30)); // 240
发布评论

评论列表(0)

  1. 暂无评论