JS time processing script Summary

Source: Internet
Author: User

Js Code ---
* Obtain the start date and end date of this week, this quarter, this month, and last month.
*/
Var now = new Date (); // current Date
Var nowDayOfWeek = now. getDay (); // the day of the week today
Var nowDay = now. getDate (); // current day
Var nowMonth = now. getMonth (); // current month
Var nowYear = now. getYear (); // current year
NowYear + = (nowYear <2000 )? 1900: 0 ;//
Var lastMonthDate = new Date (); // Date of the previous month
LastMonthDate. setDate (1 );
LastMonthDate. setMonth (lastMonthDate. getMonth ()-1 );
Var lastYear = lastMonthDate. getYear ();
Var lastMonth = lastMonthDate. getMonth ();
// Pattern Date: yyyy-MM-dd
Function formatDate (date ){
Var myyear = date. getFullYear ();
Var mymonth = date. getMonth () + 1;
Var myweekday = date. getDate ();
If (mymonth <10 ){
Mymonth = "0" + mymonth;
}
If (myweekday <10 ){
Myweekday = "0" + myweekday;
}
Return (myyear + "-" + mymonth + "-" + myweekday );
}
// Obtain the number of days in a month
Function getMonthDays (myMonth ){
Var monthStartDate = new Date (nowYear, myMonth, 1 );
Var monthEndDate = new Date (nowYear, myMonth + 1, 1 );
Var days = (monthEndDate-monthStartDate)/(1000*60*60*24 );
Return days;
}
// Obtain the start month of the current quarter
Function getQuarterStartMonth (){
Var quarterStartMonth = 0;
If (nowMonth <3 ){
QuarterStartMonth = 0;
}
If (2 <nowMonth & nowMonth <6 ){
QuarterStartMonth = 3;
}
If (5 <nowMonth & nowMonth <9 ){
QuarterStartMonth = 6;
}
If (nowMonth> 8 ){
QuarterStartMonth = 9;
}
Return quarterStartMonth;
}
// Obtain the start date of the week
Function getWeekStartDate (){
Var weekStartDate = new Date (nowYear, nowMonth, nowDay-nowDayOfWeek );
Return formatDate (weekStartDate );
}
// Obtain the end date of the week
Function getWeekEndDate (){
Var weekEndDate = new Date (nowYear, nowMonth, nowDay + (6-nowDayOfWeek ));
Return formatDate (weekEndDate );
}
// Obtain the start date of this month
Function getMonthStartDate (){
Var monthStartDate = new Date (nowYear, nowMonth, 1 );
Return formatDate (monthStartDate );
}
// Obtain the end date of this month
Function getMonthEndDate (){
Var monthEndDate = new Date (nowYear, nowMonth, getMonthDays (nowMonth ));
Return formatDate (monthEndDate );
}
// Obtain the start time of the previous month
Function getLastMonthStartDate (){
Var lastMonthStartDate = new Date (nowYear, lastMonth, 1 );
Return formatDate (lastMonthStartDate );
}
// Get the last month stop time
Function getLastMonthEndDate (){
Var lastMonthEndDate = new Date (nowYear, lastMonth, getMonthDays (lastMonth ));
Return formatDate (lastMonthEndDate );
}
// Obtain the start date of the current quarter
Function getQuarterStartDate (){
Var quarterStartDate = new Date (nowYear, getQuarterStartMonth (), 1 );
Return formatDate (quarterStartDate );
}
// Or the end date of the current quarter
Function getQuarterEndDate (){
Var quarterEndMonth = getQuarterStartMonth () + 2;
Var quarterStartDate = new Date (nowYear, quarterEndMonth, getMonthDays (quarterEndMonth ));
Return formatDate (quarterStartDate );
}

 

/*************************************** **************************************** **************************************** *
Requirement displayed on the page = get the current day-week-month
Copy and paste to run
[Html]
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> untitled document </title>
<Script language = "javascript" type = "text/javascript">
Var now = new Date (); // current Date
Var nowDayOfWeek = now. getDay (); // the day of the week today
Var nowDay = now. getDate (); // current day
Var nowMonth = now. getMonth (); // current month
Var nowYear = now. getYear (); // current year
NowYear + = (nowYear <2000 )? 1900: 0 ;//

Function getDate (){
Var myDate = new Date ();
Alert (formatDate (myDate ));
}
Function getWeek (){
Var weekStartDate = new Date (nowYear, nowMonth, nowDay-nowDayOfWeek );
Var weekEndDate = new Date (nowYear, nowMonth, nowDay + (6-nowDayOfWeek ));
Alert (formatDate (weekStartDate) + "|" + formatDate (weekEndDate ));
}
Function getMonth (){
Var monthStartDate = new Date (nowYear, nowMonth, 1 );
Var monthEndDate = new Date (nowYear, nowMonth, getMonthDays (nowMonth ));
Alert (formatDate (monthStartDate) + "|" + formatDate (monthEndDate ));
}

Function getMonthDays (myMonth ){
Var monthStartDate = new Date (nowYear, myMonth, 1 );
Var monthEndDate = new Date (nowYear, myMonth + 1, 1 );
Var days = (monthEndDate-monthStartDate)/(1000*60*60*24 );
Return days;
}
Function formatDate (date ){
Var myyear = date. getFullYear ();
Var mymonth = date. getMonth () + 1;
Var myweekday = date. getDate ();

If (mymonth <10 ){
Mymonth = "0" + mymonth;
}
If (myweekday <10 ){
Myweekday = "0" + myweekday;
}
Return (myyear + "-" + mymonth + "-" + myweekday );
}
</Script>
</Head>
 
<Body>
<Input type = "button" onclick = "getDate ()" value = "getdate"/>
<Input type = "button" onclick = "getWeek ()" value = "getWeek"/>
<Input type = "button" onclick = "getMonth ()" value = "getMonth"/>
</Body>
</Html>

 

From Xwin1989

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.