我有一个网页,每天,每月和每年有三个下拉菜单.如果我使用Date带有数字的JavaScript 构造函数,那么我得到一个Date当前时区的对象:
new Date(xiYear, xiMonth, xiDate)
Run Code Online (Sandbox Code Playgroud)
给出正确的日期,但由于夏令时,它认为日期是GMT + 01:00.
这里的问题是我然后将它传递Date给Ajax方法,当日期在服务器上反序列化时,它已经转换为GMT,因此丢失了一个小时,将一天移回一.现在我可以将日,月和年单独传递到Ajax方法中,但似乎应该有更好的方法.
接受的答案指出了我正确的方向,但只是单独使用setUTCHours()改变了:
Apr 5th 00:00 GMT+01:00
Run Code Online (Sandbox Code Playgroud)
至
Apr 4th 23:00 GMT+01:00
Run Code Online (Sandbox Code Playgroud)
然后我还必须设置UTC日期,月份和年份以结束
Apr 5th 01:00 GMT+01:00
Run Code Online (Sandbox Code Playgroud)
这就是我想要的.
我将特定时区的日期时间作为字符串,我想将其转换为本地时间.但是,我不知道如何在Date对象中设置时区.
例如,Feb 28 2013 7:00 PM ET,我可以
var mydate = new Date();
mydate.setFullYear(2013);
mydate.setMonth(02);
mydate.setDate(28);
mydate.setHours(7);
mydate.setMinutes(00);
Run Code Online (Sandbox Code Playgroud)
据我所知,我可以设置UTC时间或本地时间.但是,我如何在另一个时区设置时间?
我试图使用添加/减去UTC的偏移量,但我不知道如何应对夏令时.我不确定我是否正朝着正确的方向前进.
如何在javascript中将时间从不同的时区转换为当地时间?
<input id="datePicker" type="date" />?
Run Code Online (Sandbox Code Playgroud)
我将在chrome中设置datepicker输入类型日期的今天日期.
$(document).ready( function() {
var now = new Date();
var today = now.getDate() + '/' + (now.getMonth() + 1) + '/' + now.getFullYear();
alert(today);
$('#datePicker').val(today);
});?
Run Code Online (Sandbox Code Playgroud)
但它不起作用
编辑此jsFiddle - 请尝试使用chrome.
为什么moment.js UTC总是显示错误的日期.例如来自chrome的开发者控制台:
moment(('07-18-2013')).utc().format("YYYY-MM-DD").toString()
// or
moment.utc(new Date('07-18-2013')).format("YYYY-MM-DD").toString()
Run Code Online (Sandbox Code Playgroud)
他们两人都会回归"2013-07-17",为什么它会回归17号而不是18号,这是传递给他们的.
但如果我使用没有utc的momentjs:
moment(new Date('07-18-2013')).format("YYYY-MM-DD").toString()
Run Code Online (Sandbox Code Playgroud)
我回来了"2013-07-18",这也是我在使用moment.js UTC时所期望的.
这是否意味着我们在使用moment.js UTC时无法获得正确的日期?
我有一个以毫秒为单位的UTC日期,我将其传递给Angular的日期过滤器以进行人工格式化.
{{someDate | date:'d MMMM yyyy'}}
Run Code Online (Sandbox Code Playgroud)
太棒了,除了someDate在UTC中,日期过滤器认为它在当地时间.
我如何告诉Angular someDate是UTC?
谢谢.
我正在使用日期过滤器以某种格式呈现unix时间戳.我注意到过滤器将本地时区添加到输出中.
有没有办法简单地输出确切的时间戳,而不添加任何时区信息?
输入:
talk.content.date_and_time = 1400167800
Run Code Online (Sandbox Code Playgroud)
(是05/15/14 @ 3:30:00 pm UTC)
码:
{{talk.content.date_and_time*1000 | date:'dd-M-yyyy H:mm Z'}}
Run Code Online (Sandbox Code Playgroud)
输出:
15-5-2014 17:30 +0200
Run Code Online (Sandbox Code Playgroud)
如何输出15:30而不是17:30?
如何解析一个简单的日期字符串,让JavaScript知道它实际上是一个UTC日期?目前,如果我这样做new Date('2015-08-27'),将其转换为我的时区.
正如标题所说,我想获取某个时区一天的开始时间和结束时间,并将它们转换为 UTC 时间。这是我的实现部分:
//convert current local time to specified timezone time
var converted = moment.tz(moment(), timezone).format("YYYY-MM-DD");
var full_format = "YYYY-MM-DD HH:mm:ss";
// get start time and end time in the timezone
var start = converted + " 00:00:00";
var end = converted + " 23:59:59";
// how to convert them in utc time by momentjs or other methods?
var utc_start_time = ?
var utc_end_time = ?
Run Code Online (Sandbox Code Playgroud)
问题是如何将特定时区的时间转换为UTC时间。或者有没有其他像样的解决方案?谢谢!
编辑:
我想出了一种自己制作的方法,但不太体面。
var converted = moment.tz(moment(), timezone).format("YYYY-MM-DD");
var full_format = "YYYY-MM-DD HH:mm:ss";
var …Run Code Online (Sandbox Code Playgroud) 我试图将当前的UTC日期存储在我的数据库中.我当地时间是下午9:11这相当于UTC时间上午1:11.当我查看我的数据库时,我注意到下午1:11正在写入.我糊涂了.为了在JavaScript中获取UTC时间,我使用以下代码:
var currentDate = new Date();
var utcDate = Date.UTC(currentDate.getFullYear(), currentDate.getMonth(), currentDate.getDate(), currentDate.getHours(), currentDate.getMinutes(), currentDate.getSeconds(), currentDate.getMilliseconds());
var result = new Date(utcDate);
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
I want to get utc date using moment or any other possibilities. utcString = moment.utc(someDate)).format() is giving utc string not utc date. I am not able to perform operations like utcString.getDate() , etc using this. Please help me in providing with utc date object.
moment.utc(new Date()).toDate() 正在将日期再次转换为我的当地时间。
例如:我希望将 utc 格式的字符串"2019-10-31T00:00:00.000Z"转换为 utc 日期并执行getDate(), getFullYear()可以使用 Date() 对象执行的操作(如等)