The date calculation of JS NodeJs

Source: Internet
Author: User
Tags date1 etcd

Catalogue [-]

    • Date-utils
    • static method Methods
    • Instance Methods Interface method
Date-utils

Front-end references

<script type= "Text/javascript" src= "Date-utils.min.js" ></script>

Download portal, bash me

Nodejs service-side project invocation

$ cnpm Install Date-utilsrequire (' date-utils ');

Nodejs Version Requirements >0.6

Api:

static method Methods
Date.today ();  // today, 00:00:00date.yesterday (); // yesterday, 00:00:00    date.tomorrow ();  // tomorrow, 00:00:00date.validateday (Day, year, month);  // true/false whether a date is validdate.validateyear (year);  //  true/false whether a year is validdate.validatemonth (month);  // true/ False whether a month is validdate.validatehour (hour); // true/false  Whether an hour is validdate.validateminute (minute);  // true/false whether  a minute is validdate.validatesecond (second);  // true/false whether a  second is validdate.validatemillisecond (millisecond);  // true/false whether  a millisecond is validdate.compare (DATE1,&NBSP;DATE2);  // -1 if date1  is smaller than date2, 0 if equal, 1 if date2 is smaller than  Date1date.equals (DATE1,&NBSP;DATE2);  // true/false if date1 is equal to  date2date.getdaynumberfromname (name); // su/sun/sunday - 0, mo/mon/monday  - 1, etcdate.getmonthnumberfromname (name);  // jan/january - 0, feb/february  - 1, etcdate.isleapyear (year);  // true/false whether the year is  a leap yeardate.getdaysinmonth (year, monthnumber);  // number of days  in the month 0-11
Instance Methods Interface method
D.clone (); // returns a new copy of date object set to  The same timed.getmonthabbr ();  // abreviated month name, jan, feb,  etcd.getmonthname (); // fill month name, january, february,  Etcd.getutcoffset ();  // returns the utc offsetd.getordinalnumber ();  // day  number of the year, 1-366  (Leap year) d.cleartime ();  // sets  time to 00:00:00d.settimetonow (); // sets time to current  Timed.toformat (format); // returns date formatted with:  // yyyy  - four digit year  // mmmm - full month name. ie  january  // mmm  - short month name. ie jan  //  mm   - zero&nBsp;padded month ie 01  // m    - month ie 1   // DDDD - Full day or week name ie Tuesday    // ddd  - abbreviated day of the week ie tue   // dd   - zero padded day ie 08  // d     - day ie 8  // hh24 - hours in 24  notation ie 18  // hh   - padded hours ie 06   // H    - Hours ie 6  // MI    - Padded Minutes  // SS   - Padded Seconds    pp   - am or pm  // p    - am &NBSP;OR&NBSP;PMD.TOYMD (SeparatOR);  // returns yyyy-mm-dd by default, separator changes delimiter     d.between (DATE1,&NBSP;DATE2);  // true/false if the date/time  is between date1 and date2    d.compareto (date);  // -1  if date is smaller than this, 0 if equal, 1 if  Date is larger than thisd.equals (date); // true/false, true if  Dates are equal    d.isbefore (date);  // true/false, true if  this is before date passed    d.isafter (date);  // true /false, true if this is after date passed     D.getdaysbetween (date);  // returns number of full days between this  and passed    d.gethoursbetween (date); // returns number of hours days  Between this and passed    d.getminutesbetween (date);  // returns  number of full minutes between this and passed     d.getsecondsbetween (date);  // returns number of full seconds between  this and passed    d.add ({ milliseconds: 30,//This is a fantastic thing, it solves the computational problem.         minutes: 1,         hours: 4,        seconds: 30,         days: 2,        weeks: 1,         months: 3,        years :  2});  // adds time to existing time                 d.addmilliseconds (number);  // add milliseconds to existing  time                 D.addseconds (number); // add seconds to existing time                 d.addminutes (number);  // add  minutes to existing time                 d.addhours (number);  // add hours to existing  timed.adddays (number); // add days to existing time                 d.addweeks (number);  //  add weeks to existing time                 D.addmonths (number);  // add months to existing timed.addyears (number);  //  add years to existing time                 d.remove (...);  // same idea as for add//here is subtraction                  d.removemilliseconds (number);  // ...// same  api, just remove instead

Static calls must be Date.today () Jiangzi

Calls to Dynamic methods

var today=new Date ();

Today.add ({});

I also added two methods to dynamically get the function of the week.

?
1234567891011121314 Date.prototype.getWeekOfYear = function() {//这天在本年是第几周    varonejan = newDate(this.getFullYear(), 0, 1);    returnMath.ceil((((this- onejan) / 86400000) + onejan.getDay() + 1) / 7);};Date.prototype.getWeekOfMonth=function() {//这天在本月是第几周    var day = this.getDate();    //get weekend date    day += (this.getDay() == 0 ? 0 : 7 - this.getDay());    returnMath.ceil(parseFloat(day) / 7);};

PS: Up to 6 cycles in one months

> Console.log ((New Date ()). Toformat ("Yyyy-mm-dd HH:MI:SS"))
2016-03-17 09:46:10

The date calculation of JS NodeJs

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.