Js: a small example of getting the week number and day of the specified date

Source: Internet
Author: User

When JS obtains a date, it encounters the following requirement: Obtain the date of a week based on a week of a year. For example, the start date must be from Thursday to Friday of the next week.

The Code is as follows:

Function getNowFormatDate (theDate) {var day = theDate; var Year = 0; var Month = 0; var Day = 0; var CurrentDate = ""; // initialization time Year = day. getFullYear (); // you can set Month = day under ie Firefox. getMonth () + 1; Day = day. getDate (); CurrentDate + = Year + "-"; if (Month> = 10) {CurrentDate + = Month + "-";} else {CurrentDate + = "0" + Month + "-";} if (Day >=10) {CurrentDate + = Day;} else {CurrentDate + = "0" + D Ay;} return CurrentDate;} function isInOneYear (_ year, _ week) {if (_ year = null | _ year = ''| _ week = null | _ week ='') {return true ;} var theYear = getXDate (_ year, _ week, 4 ). getFullYear (); if (theYear! = _ Year) {return false;} return true;} // obtain the function getDateRange (_ year, _ week) {var beginDate; var endDate; if (_ year = null | _ year = ''| _ week = null | _ week ='') {return "";} beginDate = getXDate (_ year, _ week, 4); endDate = getXDate (_ year, (_ week-0 + 1), 5); return getNowFormatDate (beginDate) + "to" + getNowFormatDate (endDate);} // This method gets the weekDay of the week (weeks) of a year (year) date function getXDate (year, weeks, weekDay) {// construct a date object with the specified year, and set the date to January 1 Of The year. // because the month in the computer starts from 0, the following constructor is available: var Date = new date (year, "0 ", "1"); // get the long integer time of the date object time var time = date. getTime (); // offset the long integer time plus the time of week N. // because the first week is the current week, there are: weeks-1, and so on // 7*24*3600000 is the number of milliseconds in a week, (the date in JS is accurate to milliseconds) time + = (weeks-1) * 7*24*3600000; // reset the date object to the time date. setTime (time); return getNextDate (date, weekDay) ;}// this method will get the date function getNextDate (nowDate, weekDay) {// 0 is Sunday, 1 is Monday ,... weekDay % = 7; var day = nowDate. getDay (); var time = nowDate. getTime (); var sub = weekDay-day; if (sub <= 0) {sub + = 7;} time + = sub * 24*3600000; nowDate. setTime (time); return nowDate ;}

For example, if you get the date of the first week of January 1, 2016, it will start from Thursday. The date range of the first week is

Provide reference code for a call:

// Date Processing function dateRange () {var _ year = $ ("# _ year "). val (); var _ week = $ ("# _ week "). val (); if (isInOneYear (_ year, _ week) {var showDate = getDateRange (_ year, _ week ); $ ("# _ dateRange_import" pai.html (showDate);} else {alert (_ year + "" + _ week + ", please reselect "); $ ("# _ week "). val ("");}}

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.