A small library for manipulating, validating and formatting JavaScript date object. Futhermore, it can helps you log your execution time by using timelook.
Current Version : v1.2.0
Install via npm or download source file directly
- npm
npm install timesolver
- source
https://github.com/sean1093/timeSolver/tree/master/src
You need to use timeSolver object to manipulate JavaScript date object.
Include timeSolver
, You can use global object in your page.
<script type="text/javascript" src="timeSolver.min.js"></script>
You also can use via require
.
const timeSolver = require('./../timeSolver');
For example, if you want to get datetime string with format "YYYYMMDD", you need to do by following:
const dateString = timeSolver.getString(new Date(), "YYYYMMDD");
You can use this basic manipulate on date, add or subtract, by different time unit(type)
/**
* Add time by time unit
*
* @param {date/string} [date] standard JavaScript Date object or date string
* @param {string} [count] a number you want to add on date
* @param {string} [type] timeSolver time unit
* @return {date} standard JavaScript Date object
*/
const afterAdd = timeSolver.add(date, count, type);
/**
* Subtract time by time unit
*
* @param {date/string} [date] standard JavaScript Date object or date string
* @param {string} [count] a number you want to subtract on date
* @param {string} [type] timeSolver time unit
* @return {date} standard JavaScript Date object
*/
const afterSubtract = timeSolver.subtract(date, count, type);
/**
* Check whether two dates equals or not
*
* @param {date/string} [date1] standard JavaScript Date object or date string
* @param {date/string} [date2] standard JavaScript Date object or date string
* @return {boolean} equals or not
*/
const result = timeSolver.equal(date1, date2);
/**
* Check whether date1 after date2 or not
*
* @param {date/string} [date1] standard JavaScript Date object or date string
* @param {date/string} [date2] standard JavaScript Date object or date string
* @return {boolean} date1 after date2 or not
*/
const result = timeSolver.after(date1, date2, type);
/**
* Check whether date after today or not
*
* @param {date/string} [date] standard JavaScript Date object or date string
* @return {boolean} date1 after today or not
*/
const result = timeSolver.afterToday(date);
/**
* Check whether date1 before date2 or not
*
* @param {date/string} [date1] standard JavaScript Date object or date string
* @param {date/string} [date2] standard JavaScript Date object or date string
* @return {boolean} date1 before date2 or not
*/
const result = timeSolver.before(date1, date2, type);
/**
* Check whether date before today or not
*
* @param {date/string} [date] standard JavaScript Date object or date string
* @return {boolean} date1 before today or not
*/
const result = timeSolver.beforeToday(date);
/**
* Count time by time unit between two date
*
* @param {date/string} [date1] standard JavaScript Date object or date string
* @param {date/string} [date2] standard JavaScript Date object or date string
* @param {string} [type] timeSolver time unit
* @return {number} return count by time unit
*/
const result = timeSolver.between(date1, date2, type);
/**
* Get date time string by different format
*
* @param {date/string} [date] standard JavaScript Date object or date string
* @param {string} [format] timeSolver string format
* @return {string} return formated string
*/
const dateString = timeSolver.getString(date, format);
Get date's weekday or month name, it will return abbr. or full name by different method
const result1 = timeSolver.getAbbrWeek(date);
const result2 = timeSolver.getFullWeek(date);
const result3 = timeSolver.getAbbrMonth(date);
const result4 = timeSolver.getFullMonth(date);
/**
* Check whether date string is valid or not and return true/ false if dateString valid or not
*
* @param {string} [dateString] date string
* @param {string} [format] timeSolver valid format
* @return {boolean} dateString is valid or not
*/
const result = timeSolver.isValid(dateString, format);
/**
* Get quarter by give month
*
* @param {number} [month] month number
* @return {number} return quarter number
*/
const quarter = timeSolver.getQuarterByMonth(month);
/**
* Get quarter's first month string by give quarter
*
* @param {number} [quarter] quarter number
* @return {number} return month number
*/
const firstMonth = timeSolver.getFirstMonthByQuarter(quarter);
-
time unit parameter abbr Millisecond "MILLISECOND" "mill" Second "SECOND" "s" Minute "MINUTE" "min" Hour "HOUR" "h" Day "DAY" "d" Month "MONTH" "m" Year "YEAR" "y"
-
format "YYYY" "YYYYMM" "YYYYMMDD" "MMDDYYYY" "HH:MM:SS" "HH:MM:SSS" "YYYY/MM/DD" "YYYY/MM/DD HH:MM:SS" "YYYY/MM/DD HH:MM:SS.SSS" "YYYY-MM-DD" "YYYY-MM-DD HH:MM:SS" "YYYY-MM-DD HH:MM:SS.SSS" "YYYY.MM.DD" "YYYY.MM.DD HH:MM:SS" "YYYY.MM.DD HH:MM:SS.SSS" "MM/DD/YYYY" "MM/DD/YYYY HH:MM:SS" "MM/DD/YYYY HH:MM:SS.SSS" "MM-DD-YYYY" "MM-DD-YYYY HH:MM:SS" "MM-DD-YYYY HH:MM:SS.SSS" "MM.DD.YYYY" "MM.DD.YYYY HH:MM:SS" "MM.DD.YYYY HH:MM:SS.SSS"
-
format "YYYY/MM/DD" "YYYY-MM-DD" "YYYY.MM.DD" "YYYY/MM/DD HH:MM:SS" "YYYY-MM-DD HH:MM:SS" "YYYY.MM.DD HH:MM:SS"
It can helps you log your execution time by using timeLook
- start timeLook
timeSolver.timeLookStart();
- set your break point with label text
timeSolver.timeLook("test");
...
timeSolver.timeLook("date basic function");
...
...
- see your time log report
timeSolver.timeLookReport();
For example, it will print on your browser console.
It will shows every execution time between your two break point, and mark the bottleneck with red color(Chrome Console).
MIT
- 1.2.0 (20180408)
- Add unit-test
- New feature: getQuarterByMonth and getFirstMonthByQuarter
- 1.1.1 (20180401)
- Add to npm
- 1.1.1 (20170718)
- Enhance function: isValid(), add three timestamp format
- 1.1.0 (20170614)
- New feature: timeLook, helps you log your execution time
- 1.0.7 (20170613)
- Bug fix: YYYYMMDD: 2017613 -> 20170613
- 1.0.6 (20170110)
- Add some new feature
- Add minify version
- 1.0.5 (20160813)
- Module load compatibility
- 1.0.4 (20160614)
- Bug fix
- 1.0.3 (20160425)
- Restructure
- 1.0.2
- Bug fix: between()
- 1.0.1
- Add new function: isValid()