UTC time converted to Beijing time

// Convert UTC time to Beijing time 
function utc2beijing (utcTime) {
     var T_pos = utcTime.indexOf ('T' );
     var Z_pos = utcTime.indexOf ('Z' );
     var year_month_day = utcTime.substr (0 , T_pos);
     var hour_minute_second = utcTime.substr (T_pos + 1, Z_pos-T_pos-1 );
     var new_datetime = year_month_day + "" + hour_minute_second; // 2017-03-31 08:02:06 

    // Process becomes timestamp 
    timestamp = new Date ( Date.parse (new_datetime)); 
    timestamp = timestamp.getTime (); 
    timestamp = timestamp / 1000;

    // Add 8 hours, Beijing time is eight time zones more than UTC time 
    var timestamp = timestamp + 8 * 60 * 60 ; 

    // Timestamp is converted to time 
    const bjTime = new Date (parseInt (timestamp) * 1000) .toLocaleString ( ) .replace (/ \ // g, '.'). replace (/ [\ u4e00- \ u9fa5] / g, '' );
     return bjTime; 
} 
// utc2beijing ('2020-04-17T02: 31: 04.000000 Z ') 
// "2020.4.17 10:31:04"

 

Guess you like

Origin www.cnblogs.com/boystao/p/12739653.html
Recommended