Js implementation date display operations (instance description), js instance description

Source: Internet
Author: User
Tags date1

Js implementation date display operations (instance description), js instance description

1. js obtains the current date (yyyy-mm-dd)

Run the following code to obtain the current date:

Var myDate = new Date (); var year = myDate. getFullYear (); // obtain the complete year (4 digits, 1970 -????) Var month = myDate. getMonth () + 1; // obtain the current month (1-12) var day = myDate. getDate (); // obtain the current day (1-31) // obtain the complete year, month, and day var newDay = year + "-" + month + "-" + day;

2. Click plus or minus (yyyy-mm-dd)

Click the two buttons to add or subtract the date. If the number of days in this month reaches the maximum, the month will automatically increase or decrease.

Var n = 0; dayChange (0) $ ("# time-add "). click (function () {n ++; dayChange (n) ;}) $ ("# time-less "). click (function () {n --; dayChange (n) ;}) function dayChange (n) {var now = new Date (); // Today var tomo = new Date (now/1000 + 86400 * n) * 1000); // tomorrow var month = tomo. getMonth () + 1; var strDate = tomo. getDate (); var seperator1 = "-"; if (month> = 1 & month <= 9) {month = "0" + month ;} if (strDate> = 0 & strDate <= 9) {strDate = "0" + strDate;} var currentdate = tomo. getFullYear () + seperator1 + month + seperator1 + strDate; $ (". center-day "2.16.html (currentdate );}

3. Obtain the time range of the current Monday and Sunday of the week.

Regardless of the current week, you can obtain the start time of the current week.

Var now = new Date (); // Today week (now); function week (now) {var nowTime = now. getTime (); var day = now. getDay (); var oneDayLong = 24*60*60*1000; // obtain the Monday var MondayTime = nowTime-(day-1) * oneDayLong; // obtain the weekend var SundayTime = nowTime + (7-day) * oneDayLong; // convert the Date var Monday = new Date (MondayTime ); var Sunday = new Date (SundayTime); var month = Monday. getMonth () + 1; var strDate = Monday. getDate (); var month1 = Sunday. getMonth () + 1; var strDate1 = Sunday. getDate (); if (month> = 1 & month <= 9) {month = "0" + month;} if (month1> = 1 & month1 <= 9) {month1 = "0" + month1;} if (strDate> = 0 & strDate <= 9) {strDate = "0" + strDate ;} if (strDate1> = 0 & strDate1 <= 9) {strDate1 = "0" + strDate1;} currentdate = Monday. getFullYear () + seperator1 + month + seperator1 + strDate + "to" + Sunday. getFullYear () + seperator1 + month1 + seperator1 + strDate1; $ (". center-day "2.16.html (currentdate); 32}

4. Click to implement weekly change

Click the "change" button to change the display range of the week. If some of them are in the next month or next year, they will be automatically displayed without errors.

Var now = new Date (); // Today var n = 0; week (now); $ ("# week-add "). click (function () {n ++; var date = new Date (now. getTime () + n * 7*24*3600*1000); week (date) ;}) $ ("# week-add "). click (function () {n --; var date = new Date (now. getTime () + n * 7*24*3600*1000); week (date) ;}) function week (now) {var nowTime = now. getTime (); var day = now. getDay (); var oneDayLong = 24*60*60*1000; // obtain the Monday var MondayTime = nowTime-(day-1) * oneDayLong; // obtain var SundayTime = nowTime + (7-day) * oneDayLong; 22 // convert the Date var Monday = new Date (MondayTime ); var Sunday = new Date (SundayTime); var month = Monday. getMonth () + 1; var strDate = Monday. getDate (); var month1 = Sunday. getMonth () + 1; var strDate1 = Sunday. getDate (); if (month> = 1 & month <= 9) {month = "0" + month;} if (month1> = 1 & month1 <= 9) {month1 = "0" + month1;} if (strDate> = 0 & strDate <= 9) {strDate = "0" + strDate ;} if (strDate1> = 0 & strDate1 <= 9) {strDate1 = "0" + strDate1;} currentdate = Monday. getFullYear () + seperator1 + month + seperator1 + strDate + "to" + Sunday. getFullYear () + seperator1 + month1 + seperator1 + strDate1; $ (". center-day "2.16.html (currentdate );}

5. Obtain the first and last days of the current month.

You can get the first and last days of the current month, and the date of the last day is not fixed. You can get the expected date.

Monthfen (0) function monthfen (n) {var now = new Date (); // Today var firstDate = new Date (now/1000 + 86400 * n * now. getDate () * 1000); // tomorrow // firstDate on the first day of the month. setDate (1); // The first day var date = new Date (firstDate); var month = date. getMonth () + 1; var strDate = "0" + date. getDate (); // the last day of the month var endDate = new Date (firstDate); endDate. setMonth (firstDate. getMonth () + 1); endDate. setDate (0); var date1 = new Date (endDate); var month1 = date1.getMonth () + 1; var strDate1 = date1.getDate (); if (month> = 1 & month <= 9) {month = "0" + month;} if (month1> = 1 & month1 <= 9) {month1 = "0" + month1;} currentdate = date. getFullYear () + seperator1 + month + seperator1 + strDate + "to" + date1.getFullYear () + seperator1 + month1 + seperator1 + strDate1; $ (". center-day "2.16.html (currentdate );}

6. Click to change the current month.

Click the button to change the current month, and the date of the last day will change automatically,

Monthfen (0) var n = 0; $ ("# month-add "). click (function () {n ++; monthfen (n) ;}) $ ("# month-less "). click (function () {n --; monthfen (n) ;}) function monthfen (n) {var now = new Date (); // Today var firstDate = new Date (now/1000 + 86400 * n * now. getDate () * 1000); // tomorrow // firstDate on the first day of the month. setDate (1); // The first day var date = new Date (firstDate); var month = date. getMonth () + 1; var strDate = "0" + date. getDate (); // the last day of the month var endDate = new Date (firstDate); endDate. setMonth (firstDate. getMonth () + 1); endDate. setDate (0); var date1 = new Date (endDate); var month1 = date1.getMonth () + 1; var strDate1 = date1.getDate (); if (month> = 1 & month <= 9) {month = "0" + month;} if (month1> = 1 & month1 <= 9) {month1 = "0" + month1;} currentdate = date. getFullYear () + seperator1 + month + seperator1 + strDate + "to" + date1.getFullYear () + seperator1 + month1 + seperator1 + strDate1; $ (". center-day "2.16.html (currentdate );}


Of course, there are still a lot of changes and algorithms about date formats. If you have any questions, leave a comment. Let's discuss them together.

Some of the operations (for example) on how to implement date display in this js example are all the content that I have shared with you. I hope to give you a reference and support for the help house.

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.