Javascript code used to obtain the week number of a specified date and the day of a week

Source: Internet
Author: User
Obtain the date of a week based on a week of a year. For example, if the start date is set to "Thursday" to "Friday" of the next week, you can refer to the following requirements when getting the JS date: Obtain the date of the 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 (); // ie under Firefox
Month = day.getMonth () + 1;
Day = day.getDate ();
CurrentDate + = Year + "-";
if (Month> = 10)
{
CurrentDate + = Month + "-";
}
else
{
CurrentDate + = "0" + Month + "-";
}
if (Day> = 10)
{
CurrentDate + = Day;
}
else
{
CurrentDate + = "0" + Day;
}
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;
}

// Get date range display
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 will get the day of the week (weekDay) of the week of the year
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, there are the following methods
var date = new Date (year, "0", "1");

// Get the long time of this date object date time
var time = date.getTime ();

// Add this long shaping time to the time offset 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, (dates in JS are accurate to milliseconds)
time + = (weeks-1) * 7 * 24 * 3600000;

// reset date object date to time
date.setTime (time);
return getNextDate (date, weekDay);
}
// This method will get the day of the week (weekDay) of a certain date (nowDate)
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"). html (showDate);
} else {
alert (_year + "year without" + _ week + "week, please select again");
$ ("#_ week"). val ("");
}
} 
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.