simpletime

Convert to and from various time formats. Get manipulated time values.

npm install simpletime
29 downloads in the last month

simpleTime

(c)Bumblehead, 2013 MIT-license

OVERVIEW:

simpletime provides methods for manipulating dates and converting dates to and from unicode format. simpletime does not provide internationalized dates -use worldtime for that.

what's good about this script:

  • format and unformat dates using unicode forms
  • create and use YMDArr objects to simplify time manipulation
  • does not depend on a library

INSTALL:

simpleTime may be downloaded directly or installed through npm.

  • npm

    $ npm install simpleTime
    
  • Direct Download

    $ git clone https://github.com/iambumblehead/simpleTime.git
    

TEST:

to run tests, use npm test from a shell.

 $ npm test

GET STARTED:

javascript file

   var SimpleTime = require('simpleTime'), t;
   t = SimpleTime.getYMDArrDate(['2013', '4', '4']);
   t = SimpleTime.getDayFromDate(t, 4);
   console.log(t.getDate()); // 8

METHODS:

  • isDateObj ( dateObj )
    boolean, is the given date object a valid date object?

  • getDateYNum ( dateObj )
    return the year number from the date object
    ex return values: 2013, 8, 488

  • getDateYStr ( dateObj )
    return the year string from the date object
    ex return values: '2013', '0008', '0488'

  • getDateMNum ( dateObj )
    return the month number from the date object
    ex return values: 12, 10, 7, 4

  • getDateMStr ( dateObj )
    return the month string from the date object
    ex return values: '12', '10', '07', '04'

  • getDateDNum ( dateObj )
    return the day number from the date object
    ex return values: 12, 10, 7, 4, 30

  • getDateDStr ( dateObj )
    return the day string from the date object
    ex return values: '12', '10', '07', '04', '30'

  • getDateYMDNumArr ( dateObj )
    return the date as array of numbers ex return value: [2013, 5, 5]

  • getDateYMDStrArr ( dateObj )
    return the date as array of strings ex return value: ['2013', '05', '05']

  • getYMDArrDate ( ymdArr )
    return the array of numbers (or strings numbers) as a date object

  • getMinFromDate ( dateObj, optNum )
    return a new date object created from the given date object, with given number of minutes added. number may be negative so that the returned date will be in the past.

    optNum: +2 goes forward two. -6 go back 6.

  • getMinFromTodayDate ( optNum )
    call getMinFromDate using new Date() and the given num value

  • getDayFromDate ( dateObj, optNum )
    return a new date object created from the given date object, with given number of days added. number may be negative so that the returned date will be in the past.

    optNum: +2 goes forward two. -6 go back 6.

  • getDayFromTodayDate ( optNum )
    call getDayFromDate using new Date() and the given num value

  • getMonthFromDate ( dateObj, optNum )
    return a new date object created from the given date object, with given number of months added. number may be negative so that the returned date will be in the past.

    optNum: +2 goes forward two. -6 go back 6.

  • getMonthFromTodayDate ( optNum )
    call getMonthFromDate using new Date() and the given num value

  • getYearFromTodayDate ( optNum )
    return a new date object created from the given date object, with given number of years added. number may be negative so that the returned date will be in the past.

    optNum: +2 goes forward two. -6 go back 6.

  • getYearFromDate ( dateObj, optNum )
    call getYearFromDate using new Date() and the given num value

  • getDaysInMonth ( mNum , yNum ) return number of days that occur in the given month for the given year

  • getMFittedYMDNumArr ( YMDNumArr )
    this method will return a YMDNumArr with a value of 'M' fitted to fall within the range of 1 and 12.

    if M > 12, M is redefined as 12 if M < 1, M is redefined as 1

  • getDFittedYMDNumArr ( YMDNumArr )
    this method will return a YMDNumArr with a value of 'D' fitted to fall within the range of days that would exist for the month created by Y and M.

    for example, [2013, 4, 32] would be returned as [2013, 4, 30]

  • getTimeBgnMonth ( dateObj )
    return a date that is defined from the given dateObj, redfined to day 1, hour 0, minute 0, second 0, millisecond 0.

  • getTimeEndMonth ( dateObj )
    return a date that is defined from the given dateObj, redfined to day 1, hour 23, minute 59, second 59, millisecond 999.

  • getFirstOfMonth ( dateObj )
    return a date that is defined from the given dateObj, redfined to day 1.

  • getLastOfMonth ( dateObj )
    return a date that is defined from the given dateObj, redfined to the last day of the month.

  • getTimeBgnDay ( dateObj )
    return a date that is defined from the given dateObj, redfined to hour 0, minute 0, second 0, millisecond 0.

  • getTimeEndDay ( dateObj )
    return a date that is defined from the given dateObj, redfined to hour 23, minute 59, second 59, millisecond 999.

  • isBeforeToday ( dateObj )
    return a boolean value, is the date object before date.now(), (regardless of hour, minute, second)?

  • parseISO8601 ( str )
    return a date object from a simple ISO formatted string date, in format yyyy.mm.dd, yyyy/mm/dd, or yyyy-mm-dd

  • parseUSEndian ( str )
    return a date object from a simple US endian formatted string date, in format mm.dd.yyyy, mm/dd/yyyy, or mm-dd-yyyy

  • yieldRangeMonthly ( bgnDateObj, endDateObj, fn )
    return a monthly array of dates within the range of bgnDate and endDate. An optional filter function may be provided as the third parameter.

    bgnDate will be the first element in the returned array

  • yieldRangeDaily ( bgnDateObj, endDateObj, fn )
    return a daily array of dates within the range of bgnDate and endDate. An optional filter function may be provided as the third parameter.

    bgnDate will be the first element in the returned array

  • applyFormatDate ( dateObj , format )
    return a date that is formatted according to the given unicode formatStr

    using this date object, new Date(1365222221485), each of the following formats would return a result as shown:

    // Fri Apr 05 2013 21:23:41 GMT-0700 (PDT)
    var date = new Date(1365222221485);
    var dateFormatLong = "MMMM d, y h:mm:ss a z";
    console.log(SimpleTime.applyFormatDate(date, dateFormatLong));
    // 'April 5, 2013 9:23:41 pm 420'
    
  • getDateAsISO ( dateObj )
    return a simplified ISO formatted date from a date object (yyyy/mm/dd)

  • getDateAsUSEndian ( dateObj )
    return a simplified ISO formatted date from a date object (mm/dd/yyyy)

  • getElapsedTime ( bgnDateObj, endDateObj )
    return an object whose properties define the elapsed time between bgnDate and endDate

    // Sun Apr 07 2013 18:08:45 GMT-0700 (PDT)
    var oldDate = new Date(1365383325444);
    // Sun Apr 07 2013 23:59:59 GMT-0700 (PDT)
    var newDate = new Date(1365404399998);   
    console.log(SimpleTime.getElapsedTime(oldDate, newDate));    
    // { ms : 21074554,  
    //   sec : 21074,  
    //   min : 351, 
    //   hour : 5, 
    //   day : 0 
    // }
    
  • getElapsedTimeFormatted ( bgnDateObj, endDateObj )
    return a string composed of the elapsed time between bgnDate and endDate: min:sec:ms (mm:ss:ms)`

  • isDatesInRange ( dateObj1, dateObj2, range )
    return true if given dates fall within the same range specified by range

    type is "month", "year" or "day"

    // Sun Apr 07 2013 18:08:45 GMT-0700 (PDT)
    var oldDate = new Date(1365383325444);
    // Sun Apr 07 2013 23:59:59 GMT-0700 (PDT)
    var newDate = new Date(1365404399998);   
    console.log(SimpleTime.isDatesInRange(oldDate, newDate, 'day')); 
    // true
    
  • extractDateFormatted ( formattedDateStr, formatStr )
    return a date object that is produced from formattedDateStr and the given formatStr

    var formatStr = 'MMMM d, y h:mm:ss a z', s;
        formattedDateStr = 'April 5, 2013 9:23:41 pm 420'; 
    s = SimpleTime.extractDateFormatted(formattedDateStr, formatStr);       
    console.log(s); // Fri Apr 05 2013 21:23:41 GMT-0700 (PDT)
    

License:

(The MIT License)

Copyright (c) 2013 Bumblehead chris@bumblehead.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

npm loves you