UNPKG

datetime-object

Version:
5 lines 12.5 kB
"use strict";var _createClass=function(){function defineProperties(target,props){for(var i=0;i<props.length;i++){var descriptor=props[i];descriptor.enumerable=descriptor.enumerable||false;descriptor.configurable=true;if("value"in descriptor)descriptor.writable=true;Object.defineProperty(target,descriptor.key,descriptor);}}return function(Constructor,protoProps,staticProps){if(protoProps)defineProperties(Constructor.prototype,protoProps);if(staticProps)defineProperties(Constructor,staticProps);return Constructor;};}();var _typeof=typeof Symbol==="function"&&typeof Symbol.iterator==="symbol"?function(obj){return typeof obj;}:function(obj){return obj&&typeof Symbol==="function"&&obj.constructor===Symbol&&obj!==Symbol.prototype?"symbol":typeof obj;};function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor)){throw new TypeError("Cannot call a class as a function");}}/* eslint-disable */;(function(moduleName,root,factory){if(typeof define==="function"&&define.amd){define(["moment"],factory);}else if((typeof exports==="undefined"?"undefined":_typeof(exports))==="object"){module.exports=factory(require("moment"));}else{window.DateTime=factory(moment);}})("DateTimeModule",undefined,function(moment){"use strict";/* eslint-enable *//* global moment */function isDef(value){return value!==null&&typeof value!=="undefined";}var DateTime=function(){function DateTime(year,month,day,hours,minutes,seconds,milliseconds){_classCallCheck(this,DateTime);if(arguments.length===0){this.mmt=moment();}else{this.mmt=moment({y:year,M:month-1,d:day,h:hours,m:minutes,s:seconds,ms:milliseconds});}}_createClass(DateTime,[{key:"locale",value:function locale(){return this.mmt.locale();}},{key:"toString",value:function toString(format){return this.mmt.format(format||DateTime.defaultOutput);}},{key:"toJSON",value:function toJSON(){return this.mmt.toJSON();}},{key:"addDays",value:function addDays(nb){return DateTime.fromMoment(this.mmt.clone().add(nb*24,"hours"));}},{key:"addMonths",value:function addMonths(nb){return DateTime.fromMoment(this.mmt.clone().add(nb,"months"));}},{key:"addYears",value:function addYears(nb){return DateTime.fromMoment(this.mmt.clone().add(nb,"years"));}},{key:"addHours",value:function addHours(nb){return DateTime.fromMoment(this.mmt.clone().add(nb,"hours"));}},{key:"addMinutes",value:function addMinutes(nb){return DateTime.fromMoment(this.mmt.clone().add(nb*60,"seconds"));}},{key:"addSeconds",value:function addSeconds(nb){return DateTime.fromMoment(this.mmt.clone().add(nb,"seconds"));}},{key:"add",value:function add(years,months,days,hours,minutes,seconds){var mmt=this.mmt.clone();mmt.add(years||0,"years").add(months||0,"months").add(days||0,"days").add(hours||0,"hours").add(minutes||0,"minutes").add(seconds||0,"seconds");return DateTime.fromMoment(mmt);}},{key:"humanize",value:function humanize(relative){var duration=moment.duration(this.mmt.diff(moment()));return duration.humanize(relative);}},{key:"day",value:function day(nb){if(arguments.length===1){this.mmt=this.mmt.date(nb);return this;}return this.mmt.date();}},{key:"month",value:function month(nb){if(arguments.length===1){this.mmt=this.mmt.month(nb);return this;}return this.mmt.month()+1;}},{key:"year",value:function year(nb){if(arguments.length===1){this.mmt=this.mmt.year(nb);return this;}return this.mmt.year();}},{key:"week",value:function week(){return this.mmt.week();}},{key:"hours",value:function hours(nb){if(arguments.length===1){this.mmt=this.mmt.hours(nb);return this;}return this.mmt.hours();}},{key:"minutes",value:function minutes(nb){if(arguments.length===1){this.mmt=this.mmt.minutes(nb);return this;}return this.mmt.minutes();}},{key:"seconds",value:function seconds(nb){if(arguments.length===1){this.mmt=this.mmt.seconds(nb);return this;}return this.mmt.seconds();}},{key:"milliSeconds",value:function milliSeconds(nb){if(arguments.length===1){this.mmt=this.mmt.milliseconds(nb);return this;}return this.mmt.milliseconds();}},{key:"dayOfWeek",value:function dayOfWeek(){if(arguments.length===1){this.mmt=this.mmt.day(arguments[0]);return this;}return this.mmt.day();}},{key:"ceil",value:function ceil(precision,key){return DateTime.fromMoment(this.mmt.clone().ceil(precision,key));}},{key:"floor",value:function floor(precision,key){return DateTime.fromMoment(this.mmt.clone().floor(precision,key));}},{key:"lastDayOfMonth",value:function lastDayOfMonth(){return DateTime.fromMoment(this.mmt.clone().endOf("month"));}},{key:"firstDayOfMonth",value:function firstDayOfMonth(){return DateTime.fromMoment(this.mmt.clone().startOf("month"));}},{key:"nearest",value:function nearest(dayOfWeek,direction){dayOfWeek=dayOfWeek||0;direction=direction||1;var date=this.mmt.toDate();var dow=this.mmt.day();var delta=0;if(direction===-1&&dayOfWeek<dow){delta=-7;}date.setDate(date.getDate()+(dayOfWeek+7-dow)%7+delta);return DateTime.fromDate(date);}},{key:"nearestSunday",value:function nearestSunday(direction){return this.nearest(0,direction);}},{key:"nearestMonday",value:function nearestMonday(direction){return this.nearest(1,direction);}},{key:"nearestTuesday",value:function nearestTuesday(direction){return this.nearest(2,direction);}},{key:"nearestWednesday",value:function nearestWednesday(direction){return this.nearest(3,direction);}},{key:"nearestThursday",value:function nearestThursday(direction){return this.nearest(4,direction);}},{key:"nearestFriday",value:function nearestFriday(direction){return this.nearest(5,direction);}},{key:"nearestSaturday",value:function nearestSaturday(direction){return this.nearest(6,direction);}},{key:"toMoment",value:function toMoment(){return this.mmt.clone();}},{key:"toDate",value:function toDate(){return this.mmt.toDate();}},{key:"toObject",value:function toObject(){return this.mmt.toObject();}},{key:"utcOffset",value:function utcOffset(){return this.mmt.utcOffset();}},{key:"toUnixEpoch",value:function toUnixEpoch(){return this.mmt.unix();}},{key:"isAfter",value:function isAfter(dt){return this.mmt.isAfter(dt.mmt);}},{key:"isBefore",value:function isBefore(dt){return this.mmt.isBefore(dt.mmt);}},{key:"isSame",value:function isSame(dt){return this.mmt.isSame(dt.mmt);}},{key:"isSameOrAfter",value:function isSameOrAfter(dt){return this.mmt.isSameOrAfter(dt.mmt);}},{key:"isSameOrBefore",value:function isSameOrBefore(dt){return this.mmt.isSameOrBefore(dt.mmt);}},{key:"isBetween",value:function isBetween(dt1,dt2,strict){// return this.mmt.isBetween(dt1, dt2, granularity || null, stric || '[]'); strict=strict||"[]";if(this.mmt.isBefore(dt1.mmt)){return false;}if(this.mmt.isAfter(dt2.mmt)){return false;}if(strict.charAt(0)==="]"&&this.mmt.isSame(dt1.mmt)){return false;}if(strict.charAt(1)==="["&&this.mmt.isSame(dt2.mmt)){return false;}return true;}},{key:"diffAsTimeSpan",value:function diffAsTimeSpan(dt){var duration=moment.duration(this.mmt.diff(dt.mmt));return TimeSpan.fromDuration(duration);}},{key:"diff",value:function diff(dt){return this.mmt.diff(dt.mmt);}},{key:"roundHours",value:function roundHours(nbHours,past){if(!isDef(past)){past=true;}var s=0;var current=this.mmt.hours();while(s+nbHours<=current){s+=nbHours;}if(!past){s+=nbHours;}var dt=DateTime.fromMoment(this.mmt.clone());dt.hours(s);dt.minutes(0);dt.seconds(0);dt.milliSeconds(0);return dt;}},{key:"roundMinutes",value:function roundMinutes(nbMinutes,past){if(!isDef(past)){past=true;}var s=0;var current=this.mmt.minutes();while(s+nbMinutes<=current){s+=nbMinutes;}if(!past){s+=nbMinutes;}var dt=DateTime.fromMoment(this.mmt.clone());dt.minutes(s);dt.seconds(0);dt.milliSeconds(0);return dt;}},{key:"roundSeconds",value:function roundSeconds(nbSeconds,past){if(!isDef(past)){past=true;}var s=0;var current=this.mmt.seconds();while(s+nbSeconds<=current){s+=nbSeconds;}if(!past){s+=nbSeconds;}var dt=DateTime.fromMoment(this.mmt.clone());dt.seconds(s);dt.milliSeconds(0);return dt;}}],[{key:"fromMoment",value:function fromMoment(mmt){var dt=new DateTime();dt.mmt=mmt;return dt;}},{key:"fromDate",value:function fromDate(dt){return DateTime.fromMoment(moment(dt));}},{key:"fromObject",value:function fromObject(jsonObject){return DateTime.fromMoment(moment(jsonObject));}},{key:"SetupLocale",value:function SetupLocale(lang,definition){if(arguments.length===1){moment.locale(lang);}else if(arguments.length===2){moment.updateLocale(lang,definition);}}/* static */},{key:"parse",value:function parse(st,formats,parseExact,utcMode){var mmt=null;if(utcMode||false){mmt=moment(st,formats,parseExact);}else{mmt=moment.utc(st,formats,parseExact);}if(mmt.isValid()){return DateTime.fromMoment(mmt);}var cause=mmt.invalidAt();var causeTxt="";if(cause===0){causeTxt="years";}else if(cause===-1){causeTxt="months";}else if(cause===-2){causeTxt="days";}else if(cause===-3){causeTxt="hours";}else if(cause===-4){causeTxt="minutes";}else if(cause===-5){causeTxt="seconds";}else if(cause===-6){causeTxt="milliseconds";}console.log("'"+st+"'","couldn't parse with '"+formats+"'","cause :",causeTxt);return null;}},{key:"parseZone",value:function parseZone(st,formats){return moment.parseZone(st,formats);}},{key:"fromUnixEpoch",value:function fromUnixEpoch(number){return DateTime.fromMoment(moment(number).utc());}},{key:"now",value:function now(){return DateTime.fromMoment(moment());}},{key:"today",value:function today(){var dt=DateTime.now();dt.mmt.hours(0).minutes(0).seconds(0);//.milliSeconds(0); return dt;}},{key:"compare",value:function compare(dt1,dt2){if(this.mmt.isSame(dt1.mmt,dt2.mmt)){return 0;}return dt1.mmt.isAfter(dt2.mmt)?1:-1;}},{key:"locale",value:function locale(lang){moment.locale(lang);}},{key:"daysInMonth",value:function daysInMonth(month,year){if(month===1||month===3||month===5||month===7||month===8||month===10||month===11){return 31;}if(month===4||month===6||month===9||month===11){return 30;}if(year%4===0&&year%100!==0||year%400===0){return 29;}return 28;}},{key:"isLeapYear",value:function isLeapYear(year){return year%4===0&&year%100!==0||year%400===0;}},{key:"fn",value:function fn(name,_fn){DateTime.prototype.name=_fn;}}]);return DateTime;}();DateTime.defaultOutput=null;DateTime.defaultLocale=null;moment.duration.fn.format=moment.duration.fn.format||function(mask){// Some common format strings var formatMasks={"default":"DD MM YYYY HH:mm:ss",shortDate:"M/D/YY",mediumDate:"MM DD, YYYY",longDate:"MM DD, YYYY",fullDate:"DD, MM, YYYY",shortTime:"H:mm TT",mediumTime:"H:mm:ss TT",longTime:"H:mm:ss TT Z",isoDate:"YYYY-MM-DD",isoTime:"hh:mm:ss",isoDateTime:"YYYY-MM-DD'T'hh:mm:ss"};var format=function(){var token=/D{1,2}|M{1,2}|YY(?:YY)?|y|([HhmsTt])\1?|[LloSZ]|"[^"]*"|'[^']*'/gi;function pad(val,len){val=String(val);len=len||2;while(val.length<len){val="0"+val;}return val;}// Regexes and supporting functions are cached through closure return function(date,mask){mask=String(formatMasks[mask]||mask||formatMasks.default);var D=date.days(),M=date.months(),y=date.years(),H=date.hours(),m=date.minutes(),s=date.seconds(),L=date.milliseconds(),flags={D:D,DD:pad(D),M:m+1,MM:pad(m+1),YY:String(y).slice(2),YYYY:y,H:H%24||0,HH:pad(H%24||0),h:H,hh:pad(H),m:M,mm:pad(M),s:s,ss:pad(s),l:pad(L,3),L:pad(L>99?Math.round(L/10):L),t:H<12?"a":"p",tt:H<12?"am":"pm",T:H<12?"A":"P",TT:H<12?"AM":"PM"};return mask.replace(token,function($0){return $0 in flags?flags[$0]:$0.slice(1,$0.length-1);});};}();return format(this,mask);};var TimeSpan=function(){function TimeSpan(year,month,day,hours,minutes,seconds){_classCallCheck(this,TimeSpan);this.duration=moment.duration({seconds:seconds||0,minutes:minutes||0,hours:hours||0,days:day||0,weeks:0,months:month||0,years:year||0});}_createClass(TimeSpan,[{key:"toDuration",value:function toDuration(){return this.duration;}},{key:"totalSeconds",value:function totalSeconds(){return parseInt(this.duration.asMilliseconds()/1000,10);}},{key:"totalMinutes",value:function totalMinutes(){return parseInt(this.duration.asMilliseconds()/60000,10);}},{key:"totalHours",value:function totalHours(){return parseFloat(this.duration.asMilliseconds()/3600000);}},{key:"totalDays",value:function totalDays(){return parseFloat(this.duration.asMilliseconds()/(3600000*24));}},{key:"totalMilliSeconds",value:function totalMilliSeconds(){return this.duration.asMilliseconds();}},{key:"humanize",value:function humanize(relative){return this.duration.humanize(relative);}},{key:"toString",value:function toString(format){return this.duration.format(format);}}],[{key:"fromDuration",value:function fromDuration(duration){var ts=new TimeSpan();ts.duration=duration;return ts;}}]);return TimeSpan;}();return DateTime;});