Format method to extend 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]: ("+ o[k]"). substr (("" + o[k]). length);
}
}
return format;
}
/**
* Convert Date object to date string
* @param Date Object
* @param whether the isfull is full date data,
* When True, format like "2000-03-05 01:05:04"
* When false, format as "2000-03-05"
* @return 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 current Date object to date string
* @param Date Object
* @param whether the isfull is full date data,
* When True, format like "2000-03-05 01:05:04"
* When false, format as "2000-03-05"
* @return Date string that meets the requirements
*/
function Getsmpformatnowdate (isfull) {
Return Getsmpformatdate (New Date (), isfull);
}
/**
* Convert Long value to date string
* @param L Long value
* @param whether the isfull is full date data,
* When True, format like "2000-03-05 01:05:04"
* When false, format as "2000-03-05"
* @return Date string that meets the requirements
*/
function Getsmpformatdatebylong (l, Isfull) {
Return Getsmpformatdate (New Date (L), isfull);
}
/**
* Convert Long value to date string
* @param L Long value
* @param pattern format string, for example: Yyyy-mm-dd hh:mm:ss
* @return Date string that meets the requirements
*/
Function Getformatdatebylong (l, pattern) {
return getformatdate (New Date (L), pattern);
}
/**
* Convert Date object to date string
* @param l Long value
* @param pattern format string, for example: Yyyy-mm-dd hh:mm:ss
* @return The required date string
*/
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"));