Skip to content

A small library for manipulating, validating and formatting JavaScript date object.

License

Notifications You must be signed in to change notification settings

sean1093/timeSolver

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

71 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

timeSolver.js

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

Getting Start

Installation

Install via npm or download source file directly

  • npm
npm install timesolver
  • source

https://github.com/sean1093/timeSolver/tree/master/src

Usage

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");

Method

Add and Subtract

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);

timeSolver time unit

Equal

/**
 * 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);

After

/**
 * 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);

After Today

/**
 * 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);

Before

/**
 * 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);

Before Today

/**
 * 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);

Between

/**
 * 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);

timeSolver time unit

Get Date String

/**
 * 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);

timeSolver string format

Get Week and Month

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);

isValid

/**
 * 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);

timeSolver valid format

getQuarterByMonth

/**
 * Get quarter by give month
 * 
 * @param {number} [month] month number
 * @return {number} return quarter number
 */
const quarter = timeSolver.getQuarterByMonth(month);

getFirstMonthByQuarter

/**
 * Get quarter's first month string by give quarter
 * 
 * @param {number} [quarter] quarter number
 * @return {number} return month number
 */
const firstMonth = timeSolver.getFirstMonthByQuarter(quarter);

timeSolver time unit

  • 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"

timeSolver string format

  • 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"

timeSolver valid format

  • 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"

timeLook

It can helps you log your execution time by using timeLook

  1. start timeLook
timeSolver.timeLookStart();
  1. set your break point with label text
timeSolver.timeLook("test");
...
timeSolver.timeLook("date basic function");
...
...
  1. see your time log report
timeSolver.timeLookReport();

For example, it will print on your browser console.

timeLook

It will shows every execution time between your two break point, and mark the bottleneck with red color(Chrome Console).

License

MIT

Update log

  • 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()