Convert UTC date time to local date time

前端 未结 30 1182
悲哀的现实
悲哀的现实 2020-11-22 01:09

From the server I get a datetime variable in this format: 6/29/2011 4:52:48 PM and it is in UTC time. I want to convert it to the current user’s browser time us

相关标签:
30条回答
  • 2020-11-22 01:47

    @Adorojan's answer is almost correct. But addition of offset is not correct since offset value will be negative if browser date is ahead of GMT and vice versa. Below is the solution which I came with and is working perfectly fine for me:

    // Input time in UTC
    var inputInUtc = "6/29/2011 4:52:48";
    
    var dateInUtc = new Date(Date.parse(inputInUtc+" UTC"));
    //Print date in UTC time
    document.write("Date in UTC : " + dateInUtc.toISOString()+"<br>");
    
    var dateInLocalTz = convertUtcToLocalTz(dateInUtc);
    //Print date in local time
    document.write("Date in Local : " + dateInLocalTz.toISOString());
    
    function convertUtcToLocalTz(dateInUtc) {
    		//Convert to local timezone
    		return new Date(dateInUtc.getTime() - dateInUtc.getTimezoneOffset()*60*1000);
    }

    0 讨论(0)
  • 2020-11-22 01:48

    This is what I'm doing to convert UTC to my Local Time:

    const dataDate = '2020-09-15 07:08:08'
    const utcDate = new Date(dataDate);
    const myLocalDate = new Date(Date.UTC(
       utcDate.getFullYear(),
       utcDate.getMonth(),
       utcDate.getDate(),
       utcDate.getHours(),
       utcDate.getMinutes()
    ));
    
    document.getElementById("dataDate").innerHTML = dataDate; 
    document.getElementById("myLocalDate").innerHTML = myLocalDate; 
    <p>UTC<p>
    <p id="dataDate"></p>
    
    <p>Local(GMT +7)<p>
    <p id="myLocalDate"></p>

    Result: Tue Sep 15 2020 14:08:00 GMT+0700 (Indochina Time).

    0 讨论(0)
  • 2020-11-22 01:49

    In case you don't mind usingmoment.js and your time is in UTC just use the following:

    moment.utc('6/29/2011 4:52:48 PM').toDate();
    

    if your time is not in utc but any other locale known to you, then use following:

    moment('6/29/2011 4:52:48 PM', 'MM-DD-YYYY', 'fr').toDate();
    

    if your time is already in local, then use following:

    moment('6/29/2011 4:52:48 PM', 'MM-DD-YYYY');
    
    0 讨论(0)
  • 2020-11-22 01:49

    In Angular I used Ben's answer this way:

    $scope.convert = function (thedate) {
        var tempstr = thedate.toString();
        var newstr = tempstr.toString().replace(/GMT.*/g, "");
        newstr = newstr + " UTC";
        return new Date(newstr);
    };
    

    Edit: Angular 1.3.0 added UTC support to date filter, I haven't use it yet but it should be easier, here is the format:

    {{ date_expression | date : format : timezone}}
    

    Angular 1.4.3 Date API

    0 讨论(0)
  • 2020-11-22 01:50

    Matt's answer is missing the fact that the daylight savings time could be different between Date() and the date time it needs to convert - here is my solution:

        function ConvertUTCTimeToLocalTime(UTCDateString)
        {
            var convertdLocalTime = new Date(UTCDateString);
    
            var hourOffset = convertdLocalTime.getTimezoneOffset() / 60;
    
            convertdLocalTime.setHours( convertdLocalTime.getHours() + hourOffset ); 
    
            return convertdLocalTime;
        }
    

    And the results in the debugger:

    UTCDateString: "2014-02-26T00:00:00"
    convertdLocalTime: Wed Feb 26 2014 00:00:00 GMT-0800 (Pacific Standard Time)
    
    0 讨论(0)
  • 2020-11-22 01:50

    Using YYYY-MM-DD hh:mm:ss format :

    var date = new Date('2011-06-29T16:52:48+00:00');
    date.toString() // "Wed Jun 29 2011 09:52:48 GMT-0700 (PDT)"
    

    For converting from the YYYY-MM-DD hh:mm:ss format, make sure your date follow the ISO 8601 format.

    Year: 
        YYYY (eg 1997)    
    Year and month: 
        YYYY-MM (eg 1997-07)
    Complete date: 
        YYYY-MM-DD (eg 1997-07-16)
    Complete date plus hours and minutes:
        YYYY-MM-DDThh:mmTZD (eg 1997-07-16T19:20+01:00)    
    Complete date plus   hours, minutes and seconds:
        YYYY-MM-DDThh:mm:ssTZD (eg 1997-07-16T19:20:30+01:00)    
    Complete date plus hours, minutes, seconds and a decimal fraction of a second
        YYYY-MM-DDThh:mm:ss.sTZD (eg 1997-07-16T19:20:30.45+01:00) where:
    
    YYYY = four-digit year
    MM   = two-digit month (01=January, etc.)
    DD   = two-digit day of month (01 through 31)
    hh   = two digits of hour (00 through 23) (am/pm NOT allowed)
    mm   = two digits of minute (00 through 59)
    ss   = two digits of second (00 through 59)
    s    = one or more digits representing a decimal fraction of a second
    TZD  = time zone designator (Z or +hh:mm or -hh:mm)
    

    Important things to note

    1. You must separate the date and the time by a T, a space will not work in some browsers
    2. You must set the timezone using this format +hh:mm, using a string for a timezone (ex. : 'UTC') will not work in many browsers. +hh:mm represent the offset from the UTC timezone.
    0 讨论(0)
提交回复
热议问题