简体   繁体   English

javascript-UTC日期对象操作

[英]javascript - UTC date object manipulation

The UTC thing is really making me crazy... I am trying to have date and time on site in UTC so it has no affect of any timezone. UTC的事情确实让我发疯……我试图在UTC上安排日期和时间,因此它不影响任何时区。

What I do, I create a date object 我要做的是创建一个日期对象

var d = new Date();

//convert it to utc
var utc = d.getTime() + (d.getTimezoneOffset() * 60000); 

var utc_date = new Date(utc);
utc_date.setHours(20,0,0)

console.log(utc_date.getTime()) // I want this to be same irrespective of timezone, but don't know why it is changing

Please guide where I am doing wrong..? 请指导我在哪里做错了..?

UPDATED: I wanted to create a dropdown of time like on http://jsfiddle.net/HNyj5/ the concept here is I use a timestamp either from client side of selected date or from db and then I generate this dropdown dynamically. 更新:我想在http://jsfiddle.net/HNyj5/上创建一个时间下拉列表,这里的概念是我从选定日期的客户端或从数据库使用时间戳,然后动态生成此下拉列表。 So I want the timestamp to be similar on both server/client thats why I am trying to use UTC date object. 因此,我希望两个服务器/客户端上的时间戳都相同,这就是为什么我尝试使用UTC日期对象的原因。

You can retrieve the UTC datetime from local time like this (example timezone = GMT+0100): 您可以像这样从本地时间检索UTC日期时间(示例时区= GMT + 0100):

var currentUTC = new Date; //=>Mon Mar 18 2013 13:53:24
currentUTC.setMinutes(currentUTC.getMinutes()+currentUTC.getTimezoneOffset();
 //=> currentUTC now: Mon Mar 18 2013 12:54:06

//or
var someUTC = new Date('1998/03/18 13:52'); //=> Wed Mar 18 1998 13:52:00
someUTC.setMinutes(currentUTC.getMinutes()+currentUTC.getTimezoneOffset();
 //=> someUTC now: Wed Mar 18 1998 12:52:00

Or as a Date Extension with a one liner: 或作为带有一个班轮的日期扩展:

Date.prototype.UTCFromLocal = function(){
  var a;
  return new Date(Date.prototype.setMinutes
          .call(a = this,a.getMinutes()+a.getTimezoneOffset()));
}
// usage (current date and time = Mon Mar 18 2013 14:08:14 GMT+0100
var d = new Date().UTCFromLocal(); //=> Mon Mar 18 2013 13:08:14

And to retrieve (from a UTC datetime) you could use: 要检索(从UTC日期时间开始),您可以使用:

Date.prototype.LocalFromUTC = function(){
  var a;
  return new Date(Date.prototype.setMinutes
          .call(a = this,a.getMinutes()-a.getTimezoneOffset()));
}

Please guide where I am doing wrong..? 请指导我在哪里做错了..?

You are building a utc_date that is a completely different time, somehow biased by the getTimezoneOffset . 您正在构建一个完全不同的时间utc_date ,该时间由于某种原因受到getTimezoneOffset Just do 做就是了

var d = new Date();
d.getTime(); // milliseconds since epoch

or 要么

Date.now();

And if you're working in UTC-land, you should use d.setUTCHours instead of the local-timezone-dependent setHours . 而且,如果您在UTC领域工作,则应使用d.setUTCHours而不是依赖于本地时区的setHours

Actually what I was expecting the JS to do was if I pass the timestamp in the Date constructor it should make object wrt that timestamp but it converts it to localtimezone which was making issues for me. 实际上,我期望JS执行的操作是,如果我在Date构造函数中传递了时间戳,它应该使该对象带有该时间戳,但它将其转换为localtimezone ,这对我来说是个问题。

So what I did for solving this problem I get the date object by passing the string of the selected date. 因此,为解决此问题所做的事情是通过传递所选日期的字符串来获取date对象。

var date = new Date(selected_date_str); rather than passing the timestamp 而不是通过时间戳

as I was making dropdown of time with UTC timestamp as its value. 因为我使用UTC时间戳记作为其值进行时间下拉。 The start hour:min of dropdown was dynamic, which I was passing as argument in the function, it was from_hr like if I want to create dropdown of time from 20:00 then I pass from_hr = 20 下拉列表的开始hour:min是动态的,这是我在函数中作为参数传递的,它是from_hr例如,如果我想从20:00开始创建时间下拉列表,则传递from_hr = 20

so now I set hour for the selected date 所以现在我为所选日期设置小时

date.setHours(from_hr, 0, 0);

then I made a utc_time variable for making the the value for dropdown 然后我做了一个utc_time变量,使下拉列表的值

var utc_time = Date.UTC(date.getFullYear(), date.getMonth(), date.getDate(), from_hr, 0, 0, 0);

this will retain in all timezones, this is what I am going to use as the base. 这将保留在所有时区中,这就是我将要用作基准的时间。 Then in the loop I was adding 15 mins in the time 然后在循环中我将时间增加了15分钟

    var count = 0;
    jQuery(elem).html('');
    while(count <= 95){
        var option = '<option value="{0}">{1}:{2}</option>'.format(utc_time/1000, ('0' + date.getHours()).slice(-2), ('0' + date.getMinutes()).slice(-2)); //here i used a format prototype, which you can find in the jsfiddle link of my question
        jQuery(elem).append(option);
        utc_time += 15 * 60 * 1000; //adding 15 mins in the utc timestamp
        date.setMinutes(date.getMinutes() + 15)
        count++; }

I was dividing the utc_time with 1000 to make it php compatible, because I was going to retrieve value from here and save in db. 我将utc_time除以1000以使其与php兼容,因为我要从此处检索值并保存在db中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM