Skip to content

Instantly share code, notes, and snippets.

/date.js
Created Feb 15, 2015

Embed
What would you like to do?
Purebasic.Date to Javascript port
/**
* Requires XDate (http://arshaw.com/xdate/)
* Requires https://gist.github.com/anonymous/8760cba88588f633d7df
*/
/**
* @description Returns the date value expressing the given parameters, or the current system date if no parameters are specified.
* @param Year
* @param Month
* @param Day
* @param Hour
* @param Minute
* @param Second
* @returns {number}
*/
function pbDate(Year, Month, Day, Hour, Minute, Second) {
return ("undefined" === typeof Year
? new XDate
: new XDate(Year, Month - 1, Day, Hour, Minute, Second)
).getTime() / 1E3
}
/**
* @description Returns the year value of the specified date.
* @param a
* @returns {number}
*/
function pbYear(a) {
return (new XDate(1E3 * a)).getFullYear()
}
/**
* @description Returns the month value of the specified date.
* @param a
* @returns {number}
*/
function pbMonth(a) {
return (new XDate(1E3 * a)).getMonth() + 1
}
/**
* @description Returns the day value of the specified date.
* @param a
* @returns {number}
*/
function pbDay(a) {
return (new XDate(1E3 * a)).getDate()
}
/**
* @description Returns the weekday of the specified date.
* @param a
* @returns {number}
*/
function pbDayOfWeek(a) {
return (new XDate(1E3 * a)).getDay()
}
/**
* @description Returns the number of days elapsed since the beginning of the year of the specified date.
* @param a
* @returns {number}
*/
function pbDayOfYear(a) {
a = new XDate(1E3 * a);
for (var d = 0, b = a.getFullYear(), c = 0; c < a.getMonth(); c++)d += XDate.getDaysInMonth(b, c);
return d += a.getDate()
}
/**
* @description Returns the hour value of the specified date.
* @param a
* @returns {number}
*/
function pbHour(a) {
return (new XDate(1E3 * a)).getHours()
}
/**
* @description Returns the minute value of the specified date.
* @param a
* @returns {number}
*/
function pbMinute(a) {
return (new XDate(1E3 * a)).getMinutes()
}
/**
* @description Returns the second value of the specified date.
* @param a
* @returns {number}
*/
function pbSecond(a) {
return (new XDate(1E3 * a)).getSeconds()
}
/**
* @description Add an amount of time to a date.
* @param _Date
* @param Type
* @param Value
* @returns {number}
*/
function pbAddDate(_Date, Type, Value) {
_Date = new XDate(1E3 * _Date);
switch (Type) {
case 0: // Will add 'Value' Years to the date
_Date.addYears(Value);
break;
case 1: // Will add 'Value' Months to the date
_Date.addMonths(Value, !0);
break;
case 2: // Will add 'Value' Weeks to the date
_Date.addWeeks(Value);
break;
case 3: // Will add 'Value' Days to the date
_Date.addDays(Value);
break;
case 4: // Will add 'Value' Hours to the date
_Date.addHours(Value);
break;
case 5: // Will add 'Value' Minutes to the date
_Date.addMinutes(Value);
break;
case 6: // Will add 'Value' Seconds to the date
_Date.addSeconds(Value)
}
return _Date.getTime() / 1E3
}
/**
* @description Returns a string representation of the given Date.
* %yyyy: Will be replaced by the year value, on 4 digits.
* %yy: Will be replaced by the year value, on 2 digits.
* %mm: Will be replaced by the month value, on 2 digits.
* %dd: Will be replaced by the day value, on 2 digits.
* %hh: Will be replaced by the hour value, on 2 digits.
* %ii: Will be replaced by the minute value, on 2 digits.
* %ss: Will be replaced by the second value, on 2 digits.
* @param Mask
* @param _Date
* @returns {*}
*/
function pbFormatDate(Mask, _Date) {
var b = new XDate(1E3 * _Date),
c = ReplaceString(Mask, "%yyyy", "" + b.getFullYear()),
c = ReplaceString(c, "%yy", Right("" + b.getFullYear(), 2)),
c = ReplaceString(c, "%mm", RSet("" + (b.getMonth() + 1), 2, "0")),
c = ReplaceString(c, "%dd", RSet("" + b.getDate(), 2, "0")),
c = ReplaceString(c, "%hh", RSet("" + b.getHours(), 2, "0")),
c = ReplaceString(c, "%ii", RSet("" + b.getMinutes(), 2, "0"));
return c = ReplaceString(c, "%ss", RSet("" + b.getSeconds(), 2, "0"))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.