標籤:
目錄[-]
- date-utils
- Static Methods 靜態方法
- Instance Methods 介面方法
date-utils
前端引用
<script type="text/javascript" src="date-utils.min.js"></script>
下載傳送門,猛擊我
NODEJS服務端項目調用
$ cnpm install date-utilsrequire(‘date-utils‘);
nodejs版本要求>0.6
API :
Static 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, date2); // -1 if date1 is smaller than date2, 0 if equal, 1 if date2 is smaller than date1Date.equals(date1, 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 介面方法
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 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 or pmd.toYMD(separator); // returns YYYY-MM-DD by default, separator changes delimiter d.between(date1, 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,//這貨忒牛逼了,解決了計算問題 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//這裡就是減法 d.removeMilliseconds(number); // ...// same API, just remove instead
靜態調用必須要Date.today() 醬紫
動態方法 的 調用
var today=new Date();
today.add({});如此
其中我還自己添加了兩個方法 用於動態擷取第幾周的功能
?
| 1234567891011121314 |
Date.prototype.getWeekOfYear = function() {//這天在本年是第幾周 var onejan = new Date(this.getFullYear(), 0, 1); return Math.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()); return Math.ceil(parseFloat(day) / 7);}; |
PS:一個月最多能有6個周序
> console.log((new Date()).toFormat("YYYY-MM-DD HH:MI:SS"))
2016-03-17 09:46:10
JS nodeJs 的日期計算