在本地时区分析JavaScript日期 [英] Parse JavaScript Date in local time zone

查看:140
本文介绍了在本地时区分析JavaScript日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图解析一个 datetime-local 输入的值,所以格式是'yyyy-mm-ddTHH:mm' / code>。我想假设我正在用户的本地时区进行解析,即如果他们进入 09:00 ,则表示他们的时区中有9am

I'm trying to parse the value of a datetime-local input so the format is 'yyyy-mm-ddTHH:mm'. I want the assumption to be that I'm parsing in the local time zone of the user, i.e. if they enter 09:00 they mean 9am in their time zone.

另外不是只是当前的时间偏移,例如如果用户在6月份输入日期,那么这意味着夏令时,无论现在是11月份和DST已经结束。

Also not just the current time offset, e.g. if the user enters a date in June then they mean daylight savings time regardless of the fact it's now November and DST has ended.

我尝试使用常规Date对象moment.js,但是假设时区是UTC。

I've tried using regular Date objects and moment.js but the assumption is always that the time zone is UTC.

有没有办法这样做?

推荐答案

// dateString is the value from datetime-local
var dateInLocal = moment(dateString, "yyyy-MM-DDThh:mm");

http://jsfiddle.net/naresha/NwawQ/

这篇关于在本地时区分析JavaScript日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆