JavaScript Custom Date function

Source: Internet
Author: User
Tags dateformat

1. Formatted Date (YYYY-MM-DD)

Code:

varDateFormat =function(date) {if(! (DateinstanceofDate)) {Date= Date.replace (/-/g, "/"); Date=Newdate (date); }    varmonth = Date.getmonth () + 1; varYear =date.getfullyear (); varDay =date.getdate (); if(Month < 10) {Month= "0" +month; }    if(Day < 10) { Day= "0" +Day ; }    returnYear + "-" + month + "-" +Day ;}
View Code

Example:

Alert (DateFormat (New Date ()));

Output: 2014-08-27

2. Get the current date and format

Code:
var GetDate = function () {
var date = new Date ();
return DateFormat (date);
}

Example:

var now = GetDate ();
$ (' input[name= ' EndTime "]). Val (now);

3. Date Added period (support increased year, month, week, day, hours, minutes, seconds)

Code:

functionDateAdd (type, number, date) {/** Function: Implement JavaScript's DateAdd function.      * Parameter: Type, string expression that represents the time interval to add.      * Parameter: number, numeric expression that indicates how many time intervals to add.      * Parameters: Date, Time object.      * Returns: the new Time object.      * var olddate = new Date ();      * var newdate = DateAdd ("D", 7,olddate); */    //d = new Date ();    if(! (DateinstanceofDate)) {Date= Date.replace (/-/g, "/"); Date=Newdate (date); }    Switch(type) { Case"Y": Date.setfullyear (date.getfullyear ()+Number );  Break;  Case"M": Date.setmonth (Date.getmonth ()+Number );  Break;  Case"W": Date.setdate (date.getdate ()+ Number * 7);  Break;  Case"D": Date.setdate (date.getdate ()+Number );  Break;  Case"H": Date.sethours (date.gethours ()+Number );  Break;  Case"M": Date.setminutes (date.getminutes ()+Number );  Break;  Case"S": Date.setseconds (date.getseconds ()+Number );  Break; default: Date.setdate (d.getdate ()+Number );  Break; }    returnDateFormat (date);}
View Code

Example:

var now = GetDate ();

$ (' input[name= ' StartTime "]). Val (DateAdd (' d ', -8,now));//current date minus 8 days

$ (' input[name= ' StartTime "]). Val (DateAdd (' y ', 1,now));//Current date plus 1 years

Cond...

JavaScript Custom Date function

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.