One wave of JavaScript date judgment script sharing and one wave of javascript

Source: Internet
Author: User

One wave of JavaScript date judgment script sharing and one wave of javascript

1. Compare two dates

var x = new Date('2015-05-25'); var y = new Date('2015-05-25');  if (x.getTime() == y.getTime()) {  It'll be true; } 

2. Whether it is between two dates

var beforeSpringDate, afterSpringDate;  for (var i = 0; i < springFestivalDays.length; i++) {  beforeSpringDate = new Date(springFestivalDays[i]);  beforeSpringDate.setDate(beforeSpringDate.getDate() - 4);  afterSpringDate = new Date(springFestivalDays[i]);  afterSpringDate.setDate(afterSpringDate.getDate() + springFestivalPeriod - 1);  if (time >= beforeSpringDate.getTime() && time <= afterSpringDate.getTime()) {   result = true;   break;  }  } 

3. One line of code to determine whether it is a leap year

var input = new Date();new Date(input.getFullYear(), 1, 29).getDate() === 29 false  var input = new Date(2012,1,1);new Date(input.getFullYear(), 1, 29).getDate() === 29 true  var input = new Date(2013,1,1);new Date(input.getFullYear(), 1, 29).getDate() === 29 false  var input = new Date(2014,1,1);new Date(input.getFullYear(), 1, 29).getDate() === 29 false  var input = new Date(2014,5,1);new Date(input.getFullYear(), 1, 29).getDate() === 29 false  var input = new Date(2014,6,1);new Date(input.getFullYear(), 1, 29).getDate() === 29 false  var input = new Date(2016,6,1);new Date(input.getFullYear(), 1, 29).getDate() === 29 true  var input = new Date(2000,6,1);new Date(input.getFullYear(), 1, 29).getDate() === 29 true 

4. Judge the status of one week before and after major holidays (National Day and Spring Festival)

var holiday = {};  function inNationalDay(date) {  var result = {  beforeNationalDay: false,  duringNationalDay: false  };  if (date) {  var month = date.getMonth() + 1,   day = date.getDate();  if (month == 9 && (day >= 24 && day <= 30)) {   result.beforeNationalDay = true;  } else if(month == 10 && (day >= 1 && day <= 7)) {   result.duringNationalDay = true;  }  }  return result; }  function inSpringFestival(date) {  var result = {  beforeSpringFestival: false,  duringSpringFestival: false  };  if (date) {  // set GMT+0800 hours(set china hour +8)  date = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 8);  var time = date.getTime();  var springFestivalDays = ['2015-02-19', '2016-02-08', '2017-01-28', '2018-02-16', '2019-02-05',   '2020-01-25', '2021-02-12', '2022-02-01', '2023-01-22', '2024-02-10'];   var springDay, springDate, beforeSpringDate, afterSpringDate;  for (var i = 0; i < springFestivalDays.length; i++) {   springDay = springFestivalDays[i];   springDate = new Date(springDay);   beforeSpringDate = new Date(springDay);   beforeSpringDate.setDate(beforeSpringDate.getDate() - 7);   afterSpringDate = new Date(springDay);   afterSpringDate.setDate(afterSpringDate.getDate() + 7);   if (time >= beforeSpringDate.getTime() && time < springDate.getTime()) {   result.beforeSpringFestival = true;   break;   } else if (time >= springDate.getTime() && time < afterSpringDate.getTime()) {   result.duringSpringFestival = true;   break;   }  }  }   return result; }  holiday.cache = {  lastUpdateDate: null,  beforeHoliday: false,  duringHoliday: false };  holiday.checkHoliday = function() {  var now = new Date();  if (!holiday.cache.lastUpdateDate) {  holiday.cache.lastUpdateDate = now;  var nationalDayResult = inNationalDay(now);  var springFestivalResult = inSpringFestival(now);  holiday.cache.beforeHoliday = nationalDayResult.beforeNationalDay || springFestivalResult.beforeSpringFestival;  holiday.cache.duringHoliday = nationalDayResult.duringNationalDay || springFestivalResult.duringSpringFestival;  } else {  var lastUpdateDate = holiday.cache.lastUpdateDate;  var cacheDate = lastUpdateDate.getFullYear() + '' + lastUpdateDate.getMonth() + '' + lastUpdateDate.getDate();  var nowDate = now.getFullYear() + '' + now.getMonth() + '' + now.getDate();  if (cacheDate != nowDate) {   holiday.cache.lastUpdateDate = now;   var nationalDayResult = inNationalDay(now);   var springFestivalResult = inSpringFestival(now);   holiday.cache.beforeHoliday = nationalDayResult.beforeNationalDay || springFestivalResult.beforeSpringFestival;   holiday.cache.duringHoliday = nationalDayResult.duringNationalDay || springFestivalResult.duringSpringFestival;  }  } };  module.exports = holiday; 

5. Determine the day of the week

<SCRIPT> var s = '1970-11-17 '; alert ("Today's Week" + "day 2011 five or six ". charAt (new Date (s ). getDay (); alert ("Week" + new Date (s ). getDay (); </SCRIPT>

You can also do this:

Var weekDay = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]; var dateStr = "2008-08-08 "; var myDate = new Date (Date. parse (dateStr. replace (/-/g, "/"); alert (weekDay [myDate. getDay ()]);

Articles you may be interested in:
  • Obtain the date of this month, three months, and this year using the js date plug-in dateHelp.
  • How to calculate the date difference in javascript
  • Obtain the js Code of the lunar calendar and the current date.
  • Native javascript implements automatic update time and date
  • JS obtains time-related functions and converts timestamp to timestamp.
  • Processing Method for displaying the date of last week and last month in javascript
  • Js prototype Extension Method (string, date, Array Extension)
  • Javascript: A simple method to compare the date size
  • Summary of javascript date formatting Methods
  • Js automatically generates the birth date based on the ID card number
  • Use JavaScript to convert strings and dates and format them.

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.