How do you convert a JavaScript date to UTC?

后端 未结 29 2080
无人共我
无人共我 2020-11-22 00:50

Suppose a user of your website enters a date range.

2009-1-1 to 2009-1-3

You need to send this date to a server for some processing, but th

相关标签:
29条回答
  • 2020-11-22 00:54

    yarn add moment

    import moment from 'moment';
    
    //local Js date to UTC using moment
    const utcDate = moment.utc(moment(new date()).format('YYYY-MM-DDTHH:mm:ss')).valueOf();
    console.log('UTC Date', utcDate);
    
    
    // UTC to local date without moment
    const localDate = convertUTCDateToLocalDate(new Date(utcDate))
    console.log('UTC Date', moment(localDate).format('MMM D, YYYY HH:mm'));
    
    
    function convertUTCDateToLocalDate(date) {
    
       let newDate = new Date(date.getTime() + date.getTimezoneOffset()*60*1000);
    
       const offset = date.getTimezoneOffset() / 60;
       const hours = date.getHours();
    
       newDate.setHours(hours - offset);
    
       return newDate;
    
    }

    0 讨论(0)
  • 2020-11-22 00:56

    You can use the following method to convert any js date to UTC:

    let date = new Date(YOUR_DATE).toISOString()
    
    // It would give the date in format "2020-06-16T12:30:00.000Z" where Part before T is date in YYYY-MM-DD format, part after T is time in format HH:MM:SS  and Z stands for UTC - Zero hour offset
    
    0 讨论(0)
  • 2020-11-22 00:57

    Another solution to convert to UTC and keep it as a date object: (It works by removing the ' GMT' part from the end of the formatted string, then putting it back into the Date constructor)

    var now = new Date();
    var now_utc = new Date(now.toUTCString().slice(0, -4));
    console.log(now_utc)

    I needed to do this to interface with a datetime picker library. But in general it's a bad idea to work with dates this way.

    Users generally want to work with datetimes in their local time, so you either update the server side code to parse datetime strings with offsets correctly, then convert to UTC (best option) or you convert to a UTC string client-side before sending to the server (like in Will Stern's answer)

    0 讨论(0)
  • 2020-11-22 00:58

    The toISOString() method returns a string in simplified extended ISO format (ISO 8601), which is always 24 or 27 characters long (YYYY-MM-DDTHH:mm:ss.sssZ or ±YYYYYY-MM-DDTHH:mm:ss.sssZ, respectively). The timezone is always zero UTC offset, as denoted by the suffix "Z".

    Source: MDN web docs

    The format you need is created with the .toISOString() method. For older browsers (ie8 and under), which don't natively support this method, the shim can be found here:

    This will give you the ability to do what you need:

    var isoDate = new Date('yourdatehere').toISOString();
    

    For Timezone work, moment.js and moment.js timezone are really invaluable tools...especially for navigating timezones between client and server javascript.

    0 讨论(0)
  • 2020-11-22 00:58

    My solution keeps the date the same no matter what timezone is set on the client-side. Maybe someone will find it useful.

    My use case:

    I'm creating a todo app, where you set date of your task. This date should remain constant no matter what timezone you're in.

    Example. You want to call your friend at 8 am on June 25th.

    You create this task 5 days before (June 20th) while you're in China.

    Then, on the same day, you fly to New York for a few days.

    Then on June 25th, while you're still in New York, you wake up at 7:30 am (which means you should receive task notification in 30 mins (even tho it's 1:30 pm already in China where you were when creating the task)

    So the task is ignoring the timezone. It means 'I want to do it at 8 am in whatever timezone I'll be in'.

    What I do is let's say 'I assume you're always in London Timezone - UTC'.

    What it means is - when the user picks some date in her/his Timezone - I convert this date to the same date in UTC. ie. You pick 8 am in China, but I convert it to 8 am in UTC.

    Then - next time you open the app - I read the date saved in UTC and convert it to the same date in your current timezone - eg. I convert 8 am in UTC to 8 am in the New York timezone.

    This solution means that the date can mean something else depending on where you are when setting it and where you're reading it, but it remains constant in a way that it 'feels' like you're always in the same timezone.

    Let's write some code:

    First - we have 2 main functions for converting from/to UTC ignoring timezone:

    export function convertLocalDateToUTCIgnoringTimezone(date: Date) {
      const timestamp = Date.UTC(
        date.getFullYear(),
        date.getMonth(),
        date.getDate(),
        date.getHours(),
        date.getMinutes(),
        date.getSeconds(),
        date.getMilliseconds(),
      );
    
      return new Date(timestamp);
    }
    
    export function convertUTCToLocalDateIgnoringTimezone(utcDate: Date) {
      return new Date(
        utcDate.getUTCFullYear(),
        utcDate.getUTCMonth(),
        utcDate.getUTCDate(),
        utcDate.getUTCHours(),
        utcDate.getUTCMinutes(),
        utcDate.getUTCSeconds(),
        utcDate.getUTCMilliseconds(),
      );
    }
    

    Then, I save/read this date like:

    function saveTaskDate(localDate: Date) {
      // I convert your local calendar date so it looks like you've picked it being in UTC somewhere around London
      const utcDate = convertLocalDateToUTCIgnoringTimezone(localDate);
      api.saveTaskDate(utcDate);
    }
    
    function readTaskDate(taskUtcDate: Date) {
      // I convert this UTC date to 'look in your local timezone' as if you were now in UTC somewhere around london
      const localDateWithSameDayAsUTC = convertUTCToLocalDateIgnoringTimezone(taskUtcDate);
    
      // this date will have the same calendar day as the one you've picked previously
      // no matter where you were saving it and where you are now
    }
    
    0 讨论(0)
  • 2020-11-22 00:58

    Even simpler

    myvar.setTime(myvar.getTime() + myvar.getTimezoneOffset() * 60000);
    
    0 讨论(0)
提交回复
热议问题