JS time format CST Turn Gmt__javascript

Source: Internet
Author: User
Tags current time getdate

In recent days, in doing Baidu map, you need to convert time format and do display, but found that the time format displayed, there was confusion, both the date and hours have changed. For example:

Raw time format: Thu Aug 20:38:54 CST 2016
Convert time format: 2016-08-19 10:38

The code used is as follows:

Thu Aug 20:38:54 CST 2016
function Gettasktime (strdate) {
    Console.log ("Original Time format:" +strdate);
    var date = new Date (strdate);
    var y = date.getfullyear (); 
    var m = date.getmonth () + 1;  
    m = m < 10? (' 0 ' + m): M;  
    var d = date.getdate ();  
    D = d < 10? (' 0 ' + D): D;  
    var h = date.gethours ();  
    var minute = Date.getminutes ();  
    minute = Minute < 10? (' 0 ' + minute): minute;
    var str = y+ "-" +m+ "-" +d+ "" +h+ ":" +minute;
    Console.log ("Conversion time Format:" +str);
    return str;
};

Lookup found that JS uses the GMT time format, and in the server to obtain the current time CST format, in the use of the new Date (strdate) conversion, converted to GMT format, there are the errors described above:

Raw time format: Thu Aug 20:38:54 CST 2016
GMT time Format: Fri Aug 2016 10:38:54 gmt+0800 (China Standard Time)
Convert time format: 2016-08-19 10:38


The solution is:

(Ref.: http://blog.csdn.net/frankcheng5143/article/details/51802518)

Segment the time string in CST format, re-stitch to GMT format, and then display

Thu Aug 20:38:54 CST 2016, time format conversion
/output format: YYYY-MM-DD HH:mm:ss Thu Aug-2016 20:38:54 gmt+0800
function ge Ttasktime (strdate) {	
    if (null==strdate | | "" ==strdate) {return
        "";
    }
    var Datestr=strdate.trim (). Split ("");
    var strgmt = datestr[0]+ "" +datestr[1]+ "" +datestr[2]+ "" +datestr[5]+ "" +datestr[3]+ "" gmt+0800 ";
    var date = new Date (Date.parse (STRGMT));
    var y = date.getfullyear ();
    var m = date.getmonth () + 1;  
    m = m < 10? (' 0 ' + m): M;
    var d = date.getdate ();  
    D = d < 10? (' 0 ' + D): D;
    var h = date.gethours ();
    var minute = Date.getminutes ();  
    minute = Minute < 10? (' 0 ' + minute): minute;
    var second = Date.getseconds ();
    Second = Second < 10? (' 0 ' + second): second;
    
    return y+ "-" +m+ "-" +d+ "" +h+ ":" +minute+ ":" +second; ";
Show Results:

Raw time Format: Thu Aug 20:38:54 CST 2016
GMT time format: Thu Aug-2016 20:38:54 gmt+0800 (China Standard Time)
  Conversion time format: 2016-08-18 20:38:54

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.