Js converts long date format to standard Date Format
[Html]
<Script language = "javascript">
// Extend the format method of Date
Date. prototype. format = function (format ){
Var o = {
"M +": this. getMonth () + 1,
"D +": this. getDate (),
"H +": this. getHours (),
"M +": this. getMinutes (),
"S +": this. getSeconds (),
"Q +": Math. floor (this. getMonth () + 3)/3 ),
"S": this. getMilliseconds ()
}
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;
}
/**
* Convert a date object to a date string.
* @ Param date object
* @ Param isFull indicates whether it is complete date data,
* If it is true, the format is as follows: "01:05:04"
* If it is false, the format is as follows: "2000-03-05"
* @ Return the date string that meets the requirements
*/
Function getSmpFormatDate (date, isFull ){
Var pattern = "";
If (isFull = true | isFull = undefined ){
Pattern = "yyyy-MM-dd hh: mm: ss ";
} Else {
Pattern = "yyyy-MM-dd ";
}
Return getFormatDate (date, pattern );
}
/**
* Convert the current date object to a date string
* @ Param date object
* @ Param isFull indicates whether it is complete date data,
* If it is true, the format is as follows: "01:05:04"
* If it is false, the format is as follows: "2000-03-05"
* @ Return the date string that meets the requirements
*/
Function getSmpFormatNowDate (isFull ){
Return getSmpFormatDate (new Date (), isFull );
}
/**
* Convert the long value to a date string.
* @ Param l long value
* @ Param isFull indicates whether it is complete date data,
* If it is true, the format is as follows: "01:05:04"
* If it is false, the format is as follows: "2000-03-05"
* @ Return the date string that meets the requirements
*/
Function getSmpFormatDateByLong (l, isFull ){
Return getSmpFormatDate (new Date (l), isFull );
}
/**
* Convert the long value to a date string.
* @ Param l long value
* @ Param pattern: a string in the format of yyyy-MM-dd hh: mm: ss.
* @ Return the date string that meets the requirements
*/
Function getFormatDateByLong (l, pattern ){
Return getFormatDate (new Date (l), pattern );
}
/**
* Convert a date object to a date string.
* @ Param l long value
* @ Param pattern: a string in the format of yyyy-MM-dd hh: mm: ss.
* @ Return the date string that meets the requirements
*/
Function getFormatDate (date, pattern ){
If (date = undefined ){
Date = new Date ();
}
If (pattern = undefined ){
Pattern = "yyyy-MM-dd hh: mm: ss ";
}
Return date. format (pattern );
}
// Alert (getSmpFormatDate (new Date (1279849429000), true ));
// Alert (getSmpFormatDate (new Date (1279849429000), false ));
// Alert (getSmpFormatDateByLong (1279829423000, true ));
Alert (getSmpFormatDateByLong (1279829423000, false ));
// Alert (getFormatDateByLong (1279829423000, "yyyy-MM "));
// Alert (getFormatDate (new Date (1279829423000), "yy-MM "));
// Alert (getFormatDateByLong (1279849429000, "yyyy-MM hh: mm "));
</Script>
<Script language = "javascript">
// Extend the format method of Date
Date. prototype. format = function (format ){
Var o = {
"M +": this. getMonth () + 1,
"D +": this. getDate (),
"H +": this. getHours (),
"M +": this. getMinutes (),
"S +": this. getSeconds (),
"Q +": Math. floor (this. getMonth () + 3)/3 ),
"S": this. getMilliseconds ()
}
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;
}
/**
* Convert a date object to a date string.
* @ Param date object
* @ Param isFull indicates whether it is complete date data,
* If it is true, the format is as follows: "01:05:04"
* If it is false, the format is as follows: "2000-03-05"
* @ Return the date string that meets the requirements
*/
Function getSmpFormatDate (date, isFull ){
Var pattern = "";
If (isFull = true | isFull = undefined ){
Pattern = "yyyy-MM-dd hh: mm: ss ";
} Else {
Pattern = "yyyy-MM-dd ";
}
Return getFormatDate (date, pattern );
}
/**
* Convert the current date object to a date string
* @ Param date object
* @ Param isFull indicates whether it is complete date data,
* If it is true, the format is as follows: "01:05:04"
* If it is false, the format is as follows: "2000-03-05"
* @ Return the date string that meets the requirements
*/
Function getSmpFormatNowDate (isFull ){
Return getSmpFormatDate (new Date (), isFull );
}
/**
* Convert the long value to a date string.
* @ Param l long value
* @ Param isFull indicates whether it is complete date data,
* If it is true, the format is as follows: "01:05:04"
* If it is false, the format is as follows: "2000-03-05"
* @ Return the date string that meets the requirements
*/
Function getSmpFormatDateByLong (l, isFull ){
Return getSmpFormatDate (new Date (l), isFull );
}
/**
* Convert the long value to a date string.
* @ Param l long value
* @ Param pattern: a string in the format of yyyy-MM-dd hh: mm: ss.
* @ Return the date string that meets the requirements
*/
Function getFormatDateByLong (l, pattern ){
Return getFormatDate (new Date (l), pattern );
}
/**
* Convert a date object to a date string.
* @ Param l long value
* @ Param pattern: a string in the format of yyyy-MM-dd hh: mm: ss.
* @ Return the date string that meets the requirements
*/
Function getFormatDate (date, pattern ){
If (date = undefined ){
Date = new Date ();
}
If (pattern = undefined ){
Pattern = "yyyy-MM-dd hh: mm: ss ";
}
Return date. format (pattern );
}
// Alert (getSmpFormatDate (new Date (1279849429000), true ));
// Alert (getSmpFormatDate (new Date (1279849429000), false ));
// Alert (getSmpFormatDateByLong (1279829423000, true ));
Alert (getSmpFormatDateByLong (1279829423000, false ));
// Alert (getFormatDateByLong (1279829423000, "yyyy-MM "));
// Alert (getFormatDate (new Date (1279829423000), "yy-MM "));
// Alert (getFormatDateByLong (1279849429000, "yyyy-MM hh: mm "));
</Script>