JS realization time format of the way to summarize _javascript skills

Source: Internet
Author: User

Copy Code code as follows:

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"));

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.