For example:
The existing string is:
Var dt = "12:20:20 ";
Step 1: convert data to date-time data
Copy codeThe Code is as follows:
Var newDt = new Date (dt. replace ("-","/"));
Step 2: format the data to "yyyy-MM-dd"
(1) Extend Date
Copy codeThe Code is as follows:
Date. prototype. format = function (format ){
Var o = {
"M +": this. getMonth () + 1, // month
"D +": this. getDate (), // day
"H +": this. getHours (), // hour
"M +": this. getMinutes (), // minute
"S +": this. getSeconds (), // second
"Q +": Math. floor (this. getMonth () + 3)/3), // quarter
"S": this. getMilliseconds () // millisecond
};
If (/(y +)/. test (format )){
Format = format. replace (RegExp. $1, (this. getFullYear () + ""). substr (4-RegExp. $1. length ));
}
For (var k in o ){
If (new RegExp ("(" + k + ")"). test (format )){
Format = format. replace (RegExp. $1, RegExp. $1. length = 1? O [k]: ("00" + o [k]). substr ("" + o [k]). length ));
}
}
Return format;
};
(2) format the time
Copy codeThe Code is as follows:
Var fmtDt = newDt. format ("yyyy-MM-dd ");
Processing time in js is too time-consuming. It's not as fast as C #.
Copy codeThe Code is as follows:
Var fmtDt = Convert. ToDateTime (dt). ToString ("yyyy-MM-dd ");