Smart.Framework Logo

static class smartJ$Date
{ } .

CLASS :: Smart DateUtils (ES6, Strict Mode)
Date Utils class for Javascript


class Methods

public static createSafeDate = ( {Integer} year, {Integer} month, {Integer} day ) => {} . {Object}
@return: {Object} Normalized Date Object as: { year: YYYY, month: 1..12, day: 1..31 }
@param: {Integer} year The Raw Year: YYYY
@param: {Integer} month The Raw Month: 1..12 ; if wrong will fix ahead or behind
@param: {Integer} day The Raw Day: 1..31 ; if wrong will fix ahead or behind
Create a Safe Date Object
public static function standardizeDate ( {Object} date ) {} . {Object}
@return: {Object} Normalized Date Object as: { year: YYYY, month: 1..12, day: 1..31 }
@param: {Object} date The instanceof Date() or the Raw Date Object that need to be safe fixed as { year: YYYY, month: 1..12, day: 1..31 }
Normalize a Date Object
public static function getIsoDate ( {Object} date, {Boolean} withTime ) {} . {String}
@return: {String} Normalized Date String as: YYYY-MM-DD or YYYY-MM-DD HH:ii:ss
@param: {Object} date The Raw Date Object as { year: YYYY, month: 1..12, day: 1..31 }
@param: {Boolean} withTime If TRUE will use the date as provided, without any normalization and will return in addition: HH:ii:ss
Get a Date Object as ISO
public static function calculateDaysOffset ( {Object} sdate1, {Object} sdate2 ) {} . {Integer}
@return: {Integer} The Date Offset in seconds between sdate1 and sdate2 as: sdate2 - sdate1
@param: {Object} sdate1 Normalized Date #1 Object as: { year: YYYY, month: MM, day: DD }
@param: {Object} sdate2 Normalized Date #2 Object as: { year: YYYY, month: MM, day: DD }
Calculate Days Offset between two dates
public static calculateMonthsOffset = ( {Object} sdate1, {Object} sdate2 ) => {} . {Integer}
@return: {Integer} The Date Offset in seconds between sdate1 and sdate2 as: sdate2 - sdate1
@param: {Object} sdate1 Normalized Date #1 Object as: { year: YYYY, month: MM, day: DD }
@param: {Object} sdate2 Normalized Date #2 Object as: { year: YYYY, month: MM, day: DD }
Calculate Months Offset between two dates
public static function addYears ( {Object} date, {Integer} years ) {} . {Object}
@return: {Object} Normalized Date Object as: { year: YYYY, month: MM, day: DD }
@param: {Object} date The Raw Date Object as { year: YYYY, month: 1..12, day: 1..31 }
@param: {Integer} years The number of Years to add or substract
Add Years to a Date Object
public static function addMonths ( {Object} date, {Integer} months ) {} . {Object}
@return: {Object} Normalized Date Object as: { year: YYYY, month: MM, day: DD }
@param: {Object} date The Raw Date Object as { year: YYYY, month: 1..12, day: 1..31 }
@param: {Integer} months The number Months to add or substract
Add Months to a Date Object
public static function addDays ( {Object} date, {Integer} days ) {} . {Object}
@return: {Object} Normalized Date Object as: { year: YYYY, month: MM, day: DD }
@param: {Object} date The Raw Date Object as { year: YYYY, month: 1..12, day: 1..31 }
@param: {Integer} days The number Days to add or substract
Add Days to a Date Object
public static function isLeapYear ( {Integer} year ) {} . {Boolean}
@return: {Boolean} TRUE if the Year is Leap or FALSE if is Not Leap
@param: {Integer} year The Year to be tested
Test if the given Year is a Leap Year or not
public static function daysInMonth ( {Integer} year, {Integer} month ) {} . {Integer}
@return: {Integer} the Number of Days in the tested month as: 28, 29, 30 or 31
@param: {Integer} year The Year to be tested
@param: {Integer} month The Month to be tested
Get the Number Of Days in a specific Month of the given Year
public static function formatDate ( {String} format, {Date} date, {Object} settings ) {} . {String}
@hint: It is similar with jQueryUI formatDate
@return: {String} The date in the above format
@param: {String} format The desired format of the date
@param: {Date} date The date value to format, from Date() object
@param: {Object} settings Attributes include: dayNamesShort string[7] - abbreviated names of the days from Sunday (optional) ; dayNames string[7] - names of the days from Sunday (optional) ; monthNamesShort string[12] - abbreviated names of the months (optional) ; monthNames string[12] - names of the months (optional)
Format a date object into a string value.
The format can be combinations of the following:
d - day of month (no leading zero) ;
dd - day of month (two digit) ;
o - day of year (no leading zeros) ;
oo - day of year (three digit) ;
D - day name short ;
DD - day name long ;
m - month of year (no leading zero) ;
mm - month of year (two digit) ;
M - month name short ;
MM - month name long ;
y - year (two digit) ;
yy - year (four digit) ;
! - Windows ticks (100ns since 01/01/0001) ;
"..." - literal text ;
'' - single quote ;
public static function determineDate ( {Mixed} date, {Mixed} defaultDate ) {} . {Mixed}
@hint: It is similar with jQueryUI determineDate
@return: {Mixed} A Date object or null if fails to validate expression
@param: {Mixed} date The Date object or date relative expression to the defaultDate
@param: {Mixed} defaultDate *Optional* null (for today) or a Date object / timestamp as default (selected) date to be used for relative expressions
Determine a date by a Date object or Expression
Valid date objects or expressions:
new Date(1937, 1 - 1, 1) :: a date in the past, as object ;
'-1y -1m -1d' :: a date in the past as relative expression to the defaultDate ;
new Date(2037, 12 - 1, 31) :: a date in the future as object ;
'1y 1m 1d' :: a date in the future as relative expression to the defaultDate ;
public static function prettySecondsToDHMS ( {Integer} numSec ) {} . {String}
@return: {String} The pretty formated time as: '4 Days, 16 Hours, 28 Minutes, 7 Seconds'
@param: {Integer} numSec The number of seconds to convert
Convert the number of seconds (unixtime) into Pretty Format: Days, Hours, Minutes, Seconds

class Properties


class Constants


Sample code: JavaScript


let d = new Date(); console.log(JSON.stringify(d, null, 2)); let dz = smartJ$Date.standardizeDate(d); console.log(JSON.stringify(dz, null, 2)); let ds = smartJ$Date.standardizeDate({ year: d.getFullYear(), month: d.getMonth()+1, day: d.getDate() }); console.log(JSON.stringify(ds, null, 2)); let iso = smartJ$Date.getIsoDate(ds); console.log(iso); let iso2 = smartJ$Date.getIsoDate(ds, true); console.log(iso2); let d1 = smartJ$Date.createSafeDate(d.getFullYear(), d.getMonth()+1, d.getDate()); console.log(JSON.stringify(d1, null, 2)); let d2 = smartJ$Date.createSafeDate(d.getFullYear(), (d.getMonth()+1)+3, d.getDate()); console.log(JSON.stringify(d2, null, 2)); let o = smartJ$Date.calculateDaysOffset(d1, d2); console.log(o); let ox = smartJ$Date.calculateDaysOffset(d2, d1); console.log(ox); let m = smartJ$Date.calculateMonthsOffset(d1, d2); console.log(m); let mx = smartJ$Date.calculateMonthsOffset(d2, d1); console.log(mx); let a1 = smartJ$Date.addYears(ds, 1); console.log(JSON.stringify(a1, null, 2)); let a2 = smartJ$Date.addMonths(ds, 12); console.log(JSON.stringify(a2, null, 2)); let a3 = smartJ$Date.addDays(ds, 365); console.log(JSON.stringify(a3, null, 2)); let fd = smartJ$Date.formatDate('yy-mm-dd', d); console.log(fd); let dd = smartJ$Date.determineDate(d); console.log(JSON.stringify(dd, null, 2));


documentation generated on: 2023-10-19 23:19:04 +0000


Smart.Framework © 2009-2023 unix-world.org