How to Convert UTC date time to local date time ?

How to Convert UTC date time to local date time ?

Asked on December 18, 2018 in Javascript.
Add Comment


  • 3 Answer(s)

    Attach ‘UTC’ to the string before applying  in javascript:

    var date = new Date('6/29/2011 4:52:48 PM UTC');
    date.toString() // "Wed Jun 29 2011 09:52:48 GMT-0700 (PDT)"
    
    Answered on December 18, 2018.
    Add Comment

    Here is a  solution:

    function convertUTCDateToLocalDate(date) {
       var newDate = new Date(date.getTime()+date.getTimezoneOffset()*60*1000);
     
       var offset = date.getTimezoneOffset() / 60;
       var hours = date.getHours();
     
       newDate.setHours(hours - offset);
     
       return newDate;
    }
    

    Method:

    var date = convertUTCDateToLocalDate(new Date(date_string_you_received));
    

    Exhibit the date based on the client:

    date.toLocaleString();
    
    Answered on December 18, 2018.
    Add Comment

    Try this solution in following format:

    For more information check here:

    Here the server should entry ‘2011-06-29T16:52:48.000Z‘ :

    var utcDate = '2011-06-29T16:52:48.000Z'; // ISO-8601 formatted date returned from server
    var localDate = new Date(utcDate);
    

    The localDate will be in the  exact time.

    Answered on December 18, 2018.
    Add Comment


  • Your Answer

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