javascript-Package Date Date class

Source: Internet
Author: User

Migration time: May 27, 2017 18:43:02author:marydonjavascript-Package Date Date class (i) format dates
//to customize the format () formatting method of the date day class<script type= "Text/javascript" >//An extension to date that converts date to a string of the specified format    //the Month (m), Day (d), hour (h), Minute (m), second (s), quarter (q) can be used with 1-2 placeholders,    //year (Y) can use 1-4 placeholders, milliseconds (S) with only 1 placeholders (1-3 digits)    //Example:    //(New Date ()). Format ("Yyyy-mm-dd HH:mm:ss. S ") ==> 2016-09-19 16:32:53.731    //(New Date ()). Format ("yyyy-m-d h:m:s:s") ==> 2016-9-19 16:40:9:955Date.prototype.Format =function(FMT) {//Author:meizz        varo = {            "m+": This. GetMonth () + 1,//Month"D+": This. GetDate (),//Day"H +": This. GetHours (),//hours"m+": This. getminutes (),//points"S+": This. getseconds (),//seconds"q+": Math.floor (( This. GetMonth () + 3)/3),//Quarterly"S": This. Getmilliseconds ()//milliseconds        }; if(/(y+)/.test (FMT)) FMT = Fmt.replace (regexp.$1, ( This. getFullYear () + ""). substr (4-regexp.$1. length));  for(varKincho)if(NewRegExp ("(" + K + ")"). Test (FMT)) FMT = Fmt.replace (regexp.$1, (regexp.$1.length = = 1)? (O[k]): (("XX" + o[k]). substr ("" +o[k])); returnFMT;    }; Window.onload=function() {        varDate =NewDate (); varb = date.tolocaledatestring ();//The format obtained is: September 19, 2016        varD = date.tolocaletimestring ();//Afternoon 4:42:46        varE = date.tolocalestring ();//September 19, 2016 PM 4:44:02        varf = date.todatestring ();//Mon Sep        varg = date.toutcstring ();//Mon, Sep 08:45:42 GMT        varh = date.tostring ();//Mon Sep 16:46:23 gmt+0800 (China Standard Time)        //Custom date formats        varc = Date. Format ("Yyyy-mm-dd HH:mm:ss");//the Format () method is customdocument.getElementById ("AA"). Value =C; };</script>

(ii) Return of the dates of the week Monday and Sunday by date

/** * return date of week Monday and Sunday according to date * @param Day * parameter date * @param num * weeks * @return Oneweek * Monday, Sunday date*/functiongetweekdate (day,num) {num= num | | 0; //return value: Date of Monday and Sunday    varOneweek = {}; //Initial Date    varInitdate = ""; //Date of interceptionInitdate = Day.split (") [0]; //IE compatibility issue, convert Yyyy-mm-dd to Yyyy/mm/ddInitdate = Initdate.replace (/-/g, "/"); //Convert string to dateInitdate =NewDate (Date.parse (initdate));//The format can only be YYYY/MM/DD        //returns the number of milliseconds that day is from 0:0 January 1, 1970    varNowtime =Initdate.gettime (); //number of days returned on the day of the week: 0 (Sunday) to 6 (Saturday)    varWeekNum =Initdate.getday (); //the number of milliseconds that the day represents    varOnedaytime = 24 * 60 * 60 * 1000; //Show Monday    varMondaytime = Nowtime-(weekNum-1) *Onedaytime; //Show Sunday    varSundaytime = Nowtime + (7-weeknum) *Onedaytime; if(0! =num) {Mondaytime+ = 7 * num *Onedaytime; Sundaytime+ = 7 * num *Onedaytime; }        //Initialize Date Time    varMonday =NewDate (mondaytime); varSunday =NewDate (sundaytime); //FormatDate is a custom formatting methodMonday = monday.formatdate (' Yyyy-mm-dd ')); Sunday= Sunday.formatdate (' Yyyy-mm-dd '); Oneweek.monday=Monday; Oneweek.sunday=Sunday; returnOneweek;}

Test:

var week = getweekdate (' 2017-05-27 ', 0);         + "," + Week. Sunday);

javascript-Package Date Date class

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.