JS calculates the specified date is the week of the week

Source: Internet
Author: User

Inadvertently in the Baidu know on the discovery of such a problem, the time to see Write a function.

First we need to make it clear that since it is the first week of the specified date, it is necessary to know what the specified date is, and it is not certain that the date will be different depending on the user, so we need to set the date as a parameter, which is determined by the user.

Let's take a look at this piece of code:

1(function(){2     /*3 * Description:4 * The start of the week is calculated differently in different countries, many other countries will Sunday as the beginning of the week5 * This code uses Chinese habit, will Monday as the beginning of the week6 * hereby stated7      */8 9     functionTodayinfo (start) {Ten         varWeeklen = 7,//7 days a week is a constant OneWeekdays = ["Day", "one", "two", "three", "four", "five", "six"], AWeekinfo = {"Week":NULL, "Day":NULL},//Initialize return information, default NULL week, week null -Oneday = 24 * 60 * 60 * 1000,//the millisecond of the day is long -Weekleave,//days remaining on the day of school theWeekstart,//start on the day of the semester -Today//Today -DateDiff,//today and the date of the school day difference -Sdate;//Day of the school, date object +         varRdatestr =/\d{4}[\/-]\d{1,2}[\/-]\d{1,2}/g;//Simple date format check: 2013/12/19 -         if(!rdatestr.test (start)) { +Alert ("Please use a valid start date!!!") "); A             returnWeekinfo; at         } -Sdate =NewDate (Start.replace ("-", "/")); -Weekstart =Sdate.getday (); -Weekstart = Weekstart = = = 0? 7:weekstart;//JS in Sunday index of 0, here to 7, easy to calculate -          -Weekleave = Weeklen-Weekstart; inToday =NewDate (); -Weekinfo.day =Weekdays[today.getday ()]; toToday =NewDate (Today.getfullyear () + "/" + (Today.getmonth () + 1) + "/" +today.getdate ()); +DateDiff = Today-sdate; -         if(DateDiff < 0) { theAlert ("Don't be kidding, you haven't started school yet!!! "); *             returnWeekinfo; $         }Panax NotoginsengDateDiff = parseint (DateDiff/oneday); -Weekinfo.week = Math.ceil ((datediff-weekleave)/Weeklen) + 1; the         returnWeekinfo; +     } A     //Test Results the     varTD = Todayinfo ("2013/12/16"); +Console.log ("Today is since 2013/12/16, the beginning of the first" + Td.week + "week, today's Week" +td.day); -TD = Todayinfo ("2013/11/11"); $Console.log ("Today is since 2013/11/11, the beginning of the first" + Td.week + "week, today's Week" +td.day); $TD = Todayinfo ("2013/09/01"); -Console.log ("Today is since 2013/09/01, the beginning of the first" + Td.week + "week, today's Week" +td.day); -TD = Todayinfo ("2013/12/29"); theConsole.log ("Today is since 2013/12/29, the beginning of the first" + Td.week + "week, today's Week" +td.day); -})();

Here are some tips on using date objects:

    1. Get total number of days before January
      1 var New Date (), total = 0; 2 a.setdate (0); 3 //  -
    2. Calculate a time difference between two dates:
      1 var New New Date ();

JS calculates the specified date is the week of the week

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.