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