How do you convert a JavaScript date to UTC ?

How do you convert a JavaScript date to UTC ?

Asked on November 30, 2018 in Javascript.
Add Comment


  • 3 Answer(s)

    There is simple method for convert of javascript date to UTC is available in javascript.

    The method something named 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”.

    refer the source if you want here

    You have to created with the .toISOString() method. For older browsers (ie8 and under), which don’t natively support this method, the shim can be found here:

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

    click here for time zone works.

    Answered on November 30, 2018.
    Add Comment

    The simple code for the given question,

    var date = new Date();
    var now_utc = Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(),
      date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds());
     
    return new Date(now_utc);
    
    Answered on November 30, 2018.
    Add Comment

    Something like this should do the trick,

    var now = new Date();
    var utc = new Date(now.getTime() + now.getTimezoneOffset() * 60000);
    

    The UTC object gives the result of local date shifted to match the UTC time.

    Answered on November 30, 2018.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.