Create a Date with a set timezone without using a string representation

前端 未结 23 1108
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-22 01:48

I have a web page with three dropdowns for day, month and year. If I use the JavaScript Date constructor that takes numbers, then I get a Date obje

相关标签:
23条回答
  • 2020-11-22 02:02
    var d = new Date(xiYear, xiMonth, xiDate);
    d.setTime( d.getTime() + d.getTimezoneOffset()*60*1000 );
    

    This answer is tailored specifically to the original question, and will not give the answer you necessarily expect. In particular, some people will want to subtract the timezone offset instead of add it. Remember though that the whole point of this solution is to hack javascript's date object for a particular deserialization, not to be correct in all cases.

    0 讨论(0)
  • 2020-11-22 02:03
    d = new Date();
    utc = d.getTime() + (d.getTimezoneOffset() * 60000);
    nd = new Date(utc + (3600000*offset));
    
    offset value base on which location time zone you would like to set 
    For India offset value +5.5,
    New York offset value -4,
    London offset value +1
    

    for all location offset Wiki List of UTC time offsets

    0 讨论(0)
  • 2020-11-22 02:03

    The easiest way that I have found to get the correct date is using datejs.

    http://www.datejs.com/

    I get my dates via Ajax in this format as a string: '2016-01-12T00:00:00'

    var yourDateString = '2016-01-12T00:00:00';
    var yourDate = new Date(yourDateString);
    console.log(yourDate);
    if (yourDate.getTimezoneOffset() > 0){
        yourDate = new Date(yourDateString).addMinutes(yourDate.getTimezoneOffset());
    }
    console.log(yourDate);
    

    Console will read:

    Mon Jan 11 2016 19:00:00 GMT-0500 (Eastern Standard Time)

    Tue Jan 12 2016 00:00:00 GMT-0500 (Eastern Standard Time)

    https://jsfiddle.net/vp1ena7b/3/

    The 'addMinutes' comes from datejs, you could probably do this in pure js on your own, but I already had datejs in my project so I found a way to use it to get the correct dates.

    I thought that this might help someone...

    0 讨论(0)
  • 2020-11-22 02:04

    using .setUTCHours() it would be possible to actually set dates in UTC-time, which would allow you to use UTC-times throughout the system.

    You cannot set it using UTC in the constructor though, unless you specify a date-string.

    Using new Date(Date.UTC(year, month, day, hour, minute, second)) you can create a Date-object from a specific UTC time.

    0 讨论(0)
  • 2020-11-22 02:05

    if you want to check the difference in a time between two dates, you can simply check if second timezone is lesser or greater from your first desired timezone and subtract or add a time.

      const currTimezone = new Date().getTimezoneOffset(); // your timezone
      const newDateTimezone = date.getTimezoneOffset(); // date with unknown timezone
    
      if (currTimezone !== newDateTimezone) {
        // and below you are checking if difference should be - or +. It depends on if unknown timezone is lesser or greater than yours
        const newTimezone = (currTimezone - newDateTimezone) * (currTimezone > newDateTimezone ? 1 : -1);
        date.setTime(date.getTime() + (newTimezone * 60 * 1000));
      }
    
    0 讨论(0)
  • 2020-11-22 02:08

    One line solution

    new Date(new Date(1422524805305).getTime() - 330*60*1000)
    

    Instead of 1422524805305, use the timestamp in milliseconds Instead of 330, use your timezone offset in minutes wrt. GMT (eg India +5:30 is 5*60+30 = 330 minutes)

    0 讨论(0)
提交回复
热议问题