Commonly used date functions in JavaScript

Source: Internet
Author: User
Tags date1 getdate

Get the first day of the Week (Monday)

function Getfirstdateofweek (thedate) {
var Firstdateofweek;
Thedate.setdate (thedate.getdate () + 1-thedate.getday ());
Firstdateofweek = thedate;
return firstdateofweek;
}


Get the last day of the Week (Sunday)

function Getlastdateofweek (thedate) {
var Lastdateofweek;
Thedate.setdate (Thedate.getdate () +7-thedate.getday ());
Lastdateofweek = thedate;
return lastdateofweek;
}

Get the first day of last week (Monday)

function Getpreviousfirstdateofweek (thedate) {
var Firstdateofweek;
Thedate.setdate (Thedate.getdate ()-6-thedate.getday ());
Firstdateofweek = thedate;
return firstdateofweek;
}

Get the last day of last week (Sunday)

function Getpreviouslastdateofweek (thedate) {
var Lastdateofweek;
Thedate.setdate (Thedate.getdate ()-thedate.getday ());
Lastdateofweek = thedate;
return lastdateofweek;
}

/---------------------------------------------------
Judging leap years

//---------------------------------------------------
Date.prototype.isleapyear = function ()
{
Return (0==this.getyear ()%4&& (This.getyear ()%100!=0) | | (This.getyear ()%400==0));
}

//---------------------------------------------------
Date formatting
Format Yyyy/yyyy/yy/yy represents year
Mm/m Month
W/W Week
dd/dd/d/d Date
hh/hh/h/h time
mm/m minutes
SS/SS/S/S seconds
//---------------------------------------------------
Date.prototype.format = function (FORMATSTR)
{
var str = FORMATSTR;
var week = [' Day ', ' one ', ' two ', ' three ', ' four ', ' five ', ' six '];

Str=str.replace (/yyyy|yyyy/,this.getfullyear ());
Str=str.replace (/yy|yy/, (this.getyear ()%) >9? ( This.getyear ()%). ToString (): ' 0 ' + (this.getyear ()% 100));

Str=str.replace (/mm/,this.getmonth () >9?this.getmonth (). ToString (): ' 0 ' + this.getmonth ());
Str=str.replace (/m/g,this.getmonth ());

Str=str.replace (/w|w/g,week[this.getday ());

Str=str.replace (/dd|dd/,this.getdate () >9?this.getdate (). ToString (): ' 0 ' + this.getdate ());
Str=str.replace (/d|d/g,this.getdate ());

Str=str.replace (/hh|hh/,this.gethours () >9?this.gethours (). ToString (): ' 0 ' + this.gethours ());
Str=str.replace (/h|h/g,this.gethours ());
Str=str.replace (/mm/,this.getminutes () >9?this.getminutes (). ToString (): ' 0 ' + this.getminutes ());
Str=str.replace (/m/g,this.getminutes ());

Str=str.replace (/ss|ss/,this.getseconds () >9?this.getseconds (). ToString (): ' 0 ' + this.getseconds ());
Str=str.replace (/s|s/g,this.getseconds ());

return str;
}

//+---------------------------------------------------
//| The number of days for two time difference date format is YYYY-MM-DD
//+---------------------------------------------------
function Daysbetween (dateone,datetwo)
{
var onemonth = dateone.substring (5,dateone.lastindexof ('-'));
var oneday = dateone.substring (Dateone.length,dateone.lastindexof ('-') +1);
var oneyear = dateone.substring (0,dateone.indexof ('-'));

var twomonth = datetwo.substring (5,datetwo.lastindexof ('-'));
var twoday = datetwo.substring (Datetwo.length,datetwo.lastindexof ('-') +1);
var twoyear = datetwo.substring (0,datetwo.indexof ('-'));

var cha= (Date.parse (onemonth+ '/' +oneday+ '/' +oneyear ')-date.parse (twomonth+ '/' +twoday+ '/' +twoyear))/86400000);
Return Math.Abs (CHA);
}


//+---------------------------------------------------
//| Date Calculation
//+---------------------------------------------------
Date.prototype.dateadd = function (strinterval, number) {
var dttmp = this;
Switch (strinterval) {
Case ' s ': return new Date (Date.parse (dttmp) + (1000 * number));
Case ' n ': return new Date (Date.parse (dttmp) + (60000 * number));
Case ' H ': return new Date (Date.parse (dttmp) + (3600000 * number));
Case ' d ': return new Date (Date.parse (dttmp) + (86400000 * number));
Case ' W ': Return new Date (Date.parse (dttmp) + ((86400000 * 7) * number));
Case ' Q ': return new Date (Dttmp.getfullyear (), (Dttmp.getmonth ()) + number*3, Dttmp.getdate (), dttmp.gethours (), Dttmp.getminutes (), dttmp.getseconds ());
Case ' m ': return new Date (Dttmp.getfullyear (), (Dttmp.getmonth ()) + number, dttmp.getdate (), dttmp.gethours (), Dttmp.getminutes (), dttmp.getseconds ());
Case ' Y ': return new Date ((Dttmp.getfullyear () + number), Dttmp.getmonth (), Dttmp.getdate (), dttmp.gethours (), Dttmp.getminutes (), dttmp.getseconds ());
}
}

The

//+---------------------------------------------------
//| compares the date difference dtend format to a date type or a valid date format string
//+------------ ---------------------------------------
Date.prototype.datediff = function (Strinterval, dtend) {
var dtstart = This
if (typeof dtend = = ' string ')//if the string is converted to a date type
{
Dtend = stringtodate (dtend);
}
Switch (strinterval) {
case ' s ': Return parseint (Dtend-dtstart)/1000);
Case ' n ': Return parseint ((Dtend-dtstart)/60000);
Case ' h ': Return parseint ((dtend-dtstart)/3600000);
Case ' d ': Return parseint ((dtend-dtstart)/86400000);
Case ' W ': Return parseint (Dtend-dtstart)/(86400000 * 7));
Case ' m ': Return (Dtend.getmonth () +1) + ((Dtend.getfullyear ()-dtstart.getfullyear ()) *12)-(Dtstart.getmonth () +1) ;
Case ' y ': return dtend.getfullyear ()-dtstart.getfullyear ();
}
}

The

//+---------------------------------------------------
//| Date output string that overloads the system's ToString method
//+--------------- ------------------------------------
date.prototype.tostring = function (Showweek)
{
var mydate= this;
var str = mydate.tolocaledatestring ();
if (Showweek)
{
var week = [' Day ', ' one ', ' two ', ' three ', ' four ', ' five ', ' six '];
Str + + ' Week ' + week[mydate.getday ()];
}
return str;
}

//+---------------------------------------------------
//| date Legality validation
//| format: yyyy-mm-dd or YYYY/MM/DD
//+ ---------------------------------------------------
Function Isvaliddate (datestr)
{
var sdate= Datestr.replace (/(^s+|s+$)/g, "); Go to both sides of the space;
if (sdate== ') return true;
//If the format satisfies the yyyy-(/) mm-(/) DD or yyyy-(/) m (/) DD or yyyy-(/) m (/) d or yyyy-(/) mm-(/) d is replaced with the '
//Database tutorial, the legal date can be: yyyy-mm/ DD (2003-3/21), the database is automatically converted to the YYYY-MM-DD format
var s = sdate.replace (/[d]{4,4}[-/]{1}[d]{1,2}[-/]{1}[d]{1,2}/g, ""); if (s== ')//description format satisfies the YYYY-MM-DD or yyyy-m-dd or yyyy-m-d or yyyy-mm-d
{
var t=new date (Sdate.replace (/-/g, '/'));
var ar = sdate.split (/[-/:]/);
if (ar[0]!= t.getyear () | | ar[1]!= t.getmonth () +1 | | ar[2]!= t.getdate ())
{
//alert (' Wrong date format! The format is: Yyyy-mm-dd or YYYY/MM/DD. Notice leap year. ');
return false;
}
}
Else
{
//alert (' Wrong date format! The format is: Yyyy-mm-dd or YYYY/MM/DD. Notice leap year. ');
return false;
}
return true;
}

//+---------------------------------------------------
//| date time check
//| format: yyyy-mm-dd hh:mm:ss
//+---- -----------------------------------------------
Function Checkdatetime (str)
{
var reg =/^ (d+)-(d{1,2})-( D{1,2}) (d{1,2}):(d{1,2}):(d{1,2}) $/;
var r = Str.match (reg);
if (r==null) return false;
R[2]=r[2]-1;
Var d= new Date (r[1],r[2],r[3],r[4],r[5],r[6]);
if (D.getfullyear ()!=r[1]) return false;
if (D.getmonth ()!=r[2]) return false;
if (d.getdate ()!=r[3]) return false;
if (d.gethours ()!=r[4]) return false;
if (d.getminutes ()!=r[5]) return false;
if (D.getseconds ()!=r[6]) return false;
return true;
}

//+---------------------------------------------------
//| to split the date into groups
//+-------------------------------- -------------------
Date.prototype.toarray = function ()
{
var mydate = this;
var myarray = array ();
Myarray[0] = Mydate.getfullyear ();
Myarray[1] = Mydate.getmonth ();
Myarray[2] = Mydate.getdate ();
Myarray[3] = mydate.gethours ();
Myarray[4] = Mydate.getminutes ();
Myarray[5] = Mydate.getseconds ();
return myarray;
}

//+---------------------------------------------------
//| Get Date data information
//| parameter interval representing data type
//| y year m month d days W Week WW Week h when N minute s sec
//+---------------------------------------------------
Date.prototype.datepart = function (int Erval)
{
var mydate = this;
var partstr= ';
var week = [' Day ', ' one ', ' two ', ' three ', ' four ', ' five ', ' six '];
Switch (interval)
{
case ' y ':p artstr = Mydate.getfullyear ();
Case ' m ':p artstr = Mydate.getmonth () +1;break;
Case ' d ':p artstr = Mydate.getdate ();
Case ' W ':p artstr = Week[mydate.getday ()];break;
case ' ww ':p artstr = Mydate.weeknumofyear ();
case ' h ':p artstr = mydate.gethours ();
Case ' n ':p artstr = Mydate.getminutes ();
case ' s ':p artstr = Mydate.getseconds ();
}
Return partstr;
}

//+---------------------------------------------------
//| Gets the maximum number of days of the month in which the current date is located
//+-------------------------- -------------------------
Date.prototype.maxdayofdate = function ()
{
var mydate = this;
var ary = Mydate.toarray ();
var date1 = (new Date (ary[0],ary[1]+1,1));
var date2 = date1.dateadd (1, ' m ', 1);
var result = DateDiff (Date1.format (' Yyyy-mm-dd '), Date2.format (' yyyy-mm-dd '));
return result;
}

//+---------------------------------------------------
//| To get the current date the week is the week ordinal of the year
//+---------------------------------------------------
Date.prototype.weeknumofyear = function ()
{
var mydate = this;
var ary = Mydate.toarray ();
var year = ary[0];
var month = ary[1]+1;
var day = ary[2];
document.write (' < script language=vbscript> n ');
document.write (' mydate = DateValue (' +month+ '-' +day+ '-' +year+ ') n ');
document.write (' result = datepart (' ww ', mydate) n ');
document.write (' n ');
return result;
}

//+---------------------------------------------------
//| string is converted to date type
//| Format mm/dd/yyyy mm-dd-yyyy yyyy/mm/dd yyyy-mm-dd
//+---------------------------------------------------
function Stringtodate (DATESTR)
{

var converted = Date.parse (DATESTR);
var mydate = new Date (converted);
if (isNaN (mydate))
{
//var Delimcahar = datestr.indexof ('/')!=-1? ' /':'-';
var arys= datestr.split ('-');
MyDate = new Date (Arys[0],--arys[1],arys[2]);
}
Return mydate;
}

Related Article

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.