'use strict'; //@ Ox.getDate Get the day of a date, optionally UTC // see Ox.setSeconds for source code /*@ Ox.getDateInWeek Get the date that falls on a given weekday in the same week # Usage (date, weekday) -> Date (date, weekday, utc) -> Date # Arguments date Date weekday 1-7 (Monday-Sunday) or name, full ("Monday") or short ("Sun") utc If true, all dates are UTC # Examples > Ox.formatDate(Ox.getDateInWeek(new Date("January 1 2000"), "Sunday"), "%A, %B %e, %Y") "Sunday, January 2, 2000" > Ox.formatDate(Ox.getDateInWeek(new Date("Jan 1 2000"), "Fri"), "%A, %B %e, %Y") "Friday, December 31, 1999" > Ox.formatDate(Ox.getDateInWeek(new Date("1/1/2000"), 1), "%A, %B %e, %Y") "Monday, December 27, 1999" @*/ // fixme: why is this Monday first? shouldn't it then be "getDateInISOWeek"?? Ox.getDateInWeek = function(date, weekday, utc) { date = Ox.makeDate(date); var sourceWeekday = Ox.getISODay(date, utc), targetWeekday = Ox.isNumber(weekday) ? weekday : Ox.indexOf(Ox.WEEKDAYS, function(v) { return v.substr(0, 3) == weekday.substr(0, 3); }) + 1; return Ox.setDate(date, Ox.getDate(date, utc) - sourceWeekday + targetWeekday, utc); } //@ Ox.getDay Get the weekday of a date, optionally UTC // see Ox.setSeconds for source code /*@ Ox.getDayOfTheYear Get the day of the year for a given date # Usage (date) -> Date (date, utc) -> Date # Arguments date Date utc If true, all dates are UTC # Examples > Ox.getDayOfTheYear(new Date("12/31/2000")) 366 > Ox.getDayOfTheYear(new Date("12/31/2002")) 365 > Ox.getDayOfTheYear(new Date("12/31/2004")) 366 @*/ Ox.getDayOfTheYear = function(date, utc) { date = Ox.makeDate(date); var month = Ox.getMonth(date, utc), year = Ox.getFullYear(date, utc); return Ox.sum(Ox.range(month).map(function(i) { return Ox.getDaysInMonth(year, i + 1); })) + Ox.getDate(date, utc); }; /*@ Ox.getDaysInMonth Get the number of days in a given month > Ox.getDaysInMonth(2000, 2) 29 > Ox.getDaysInMonth("2002", "Feb") 28 > Ox.getDaysInMonth(new Date('01/01/2004'), "February") 29 @*/ Ox.getDaysInMonth = function(year, month) { year = Ox.makeYear(year); month = Ox.isNumber(month) ? month : Ox.getIndexOf(Ox.MONTHS, function(v) { return v.substr(0, 3) == month.substr(0, 3); }) + 1; return new Date(year, month, 0).getDate(); } /*@ Ox.getDaysInYear Get the number of days in a given year > Ox.getDaysInYear(1900) 365 > Ox.getDaysInYear('2000') 366 > Ox.getDaysInYear(new Date('01/01/2004')) 366 @*/ Ox.getDaysInYear = function(year, utc) { return 365 + Ox.isLeapYear(Ox.makeYear(year, utc)); }; /*@ Ox.getFirstDayOfTheYear Get the weekday of the first day of a given year Returns the decimal weekday of January 1 (0-6, Sunday as first day) > Ox.getFirstDayOfTheYear(new Date('01/01/2000')) 6 @*/ Ox.getFirstDayOfTheYear = function(date, utc) { date = Ox.makeDate(date); date = Ox.setMonth(date, 0, utc); date = Ox.setDate(date, 1, utc); return Ox.getDay(date, utc) }; //@ Ox.getFullYear Get the year of a date, optionally UTC // see Ox.setSeconds for source code //@ Ox.getHours Get the hours of a date, optionally UTC // see Ox.setSeconds for source code /*@ Ox.getISODate Get the ISO date string for a given date > Ox.getISODate(new Date('01/01/2000')) '2000-01-01T00:00:00Z' @*/ Ox.getISODate = function(date, utc) { return Ox.formatDate(Ox.makeDate(date), '%FT%TZ', utc); }; /*@ Ox.getISODay Get the ISO weekday of a given date Returns the decimal weekday (1-7, Monday as first day) > Ox.getISODay(new Date('01/01/2000')) 6 > Ox.getISODay(new Date('01/02/2000')) 7 > Ox.getISODay(new Date('01/03/2000')) 1 @*/ Ox.getISODay = function(date, utc) { return Ox.getDay(Ox.makeDate(date), utc) || 7; }; /*@ Ox.getISOWeek Get the ISO week of a given date See ISO 8601 > Ox.getISOWeek(new Date('01/01/2000')) 52 > Ox.getISOWeek(new Date('01/02/2000')) 52 > Ox.getISOWeek(new Date('01/03/2000')) 1 @*/ Ox.getISOWeek = function(date, utc) { date = Ox.makeDate(date); // set date to Thursday of the same week return Math.floor((Ox.getDayOfTheYear(Ox.setDate( date, Ox.getDate(date, utc) - Ox.getISODay(date, utc) + 4, utc ), utc) - 1) / 7) + 1; }; /*@ Ox.getISOYear Get the ISO year of a given date See ISO 8601 > Ox.getISOYear(new Date("01/01/2000")) 1999 > Ox.getISOYear(new Date("01/02/2000")) 1999 > Ox.getISOYear(new Date("01/03/2000")) 2000 @*/ Ox.getISOYear = function(date, utc) { date = Ox.makeDate(date); // set date to Thursday of the same week return Ox.getFullYear(Ox.setDate( date, Ox.getDate(date, utc) - Ox.getISODay(date, utc) + 4, utc )); }; //@ Ox.getMilliseconds Get the milliseconds of a date // see Ox.setSeconds for source code //@ Ox.getMinutes Get the minutes of a date, optionally UTC // see Ox.setSeconds for source code //@ Ox.getMonth Get the month of a date, optionally UTC // see Ox.setSeconds for source code //@ Ox.getSeconds Get the seconds of a date // see Ox.setSeconds for source code //@ Ox.getTime Alias for +new Date() Ox.getTime = function(utc) { return +new Date() - (utc ? Ox.getTimezoneOffset() : 0); }; /*@ Ox.getTimezoneOffset Get the local time zone offset in milliseconds @*/ Ox.getTimezoneOffset = function() { return new Date().getTimezoneOffset() * 60000; }; /*@ Ox.getTimezoneOffsetString Get the local time zone offset as a string Returns a time zone offset string (from around '-1200' to around '+1200'). > Ox.getTimezoneOffsetString(new Date('01/01/2000')).length 5 @*/ Ox.getTimezoneOffsetString = function(date) { var offset = (Ox.makeDate(date)).getTimezoneOffset(); return (offset < 0 ? '+' : '-') + Ox.pad(Math.floor(Math.abs(offset) / 60), 2) + Ox.pad(Math.abs(offset) % 60, 2); }; /*@ Ox.getWeek Get the week of a given day Returns the week of the year (0-53, Sunday as first day) > Ox.getWeek(new Date('01/01/2000')) 0 > Ox.getWeek(new Date('01/02/2000')) 1 > Ox.getWeek(new Date('01/03/2000')) 1 @*/ Ox.getWeek = function(date, utc) { date = Ox.makeDate(date); return Math.floor((Ox.getDayOfTheYear(date, utc) + Ox.getFirstDayOfTheYear(date, utc) - 1) / 7); }; /*@ Ox.isLeapYear Returns true if a given year is a leap year > Ox.isLeapYear(1900) false > Ox.isLeapYear('2000') true > Ox.isLeapYear(new Date('01/01/2004')) true @*/ Ox.isLeapYear = function(year, utc) { year = Ox.makeYear(year, utc); return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0); }; /*@ Ox.makeDate Takes a date, number or string, returns a date > Ox.formatDate(Ox.makeDate(new Date('01/01/1970')), '%m/%d/%Y') '01/01/1970' > Ox.formatDate(Ox.makeDate(0), '%m/%d/%Y') '01/01/1970' > Ox.formatDate(Ox.makeDate('01/01/1970'), '%m/%d/%Y') '01/01/1970' @*/ Ox.makeDate = function(date) { // if date is a date, new Date(date) makes a clone return Ox.isUndefined(date) ? new Date() : new Date(date); }; /*@ Ox.makeYear Takes a date, number or string, returns a year > Ox.makeYear(new Date('01/01/1970')) 1970 > Ox.makeYear(1970) 1970 > Ox.makeYear('1970') 1970 @*/ Ox.makeYear = function(date, utc) { return Ox.isDate(date) ? Ox.getFullYear(date, utc) : parseInt(date); }; /*@ Ox.parseDate Takes a string ('YYYY-MM-DD HH:MM:SS') and returns a date str string utc If true, Date is UTC > +Ox.parseDate('1970-01-01 01:01:01', true) 3661000 > +Ox.parseDate('1970', true) 0 > Ox.parseDate('50', true).getUTCFullYear() 50 @*/ Ox.parseDate = function(str, utc) { var date = new Date(0), defaults = [, 1, 1, 0, 0, 0], values = /(-?\d+)-?(\d+)?-?(\d+)? ?(\d+)?:?(\d+)?:?(\d+)?/.exec(str); if (values) { values.shift(); values = values.map(function(v, i) { return v || defaults[i]; }); values[1]--; [ 'FullYear', 'Month', 'Date', 'Hours', 'Minutes', 'Seconds' ].forEach(function(part, i) { Ox['set' + part](date, values[i], utc); }); } else { date = null; } return date; }; /* Ox.parseDateRange = function(start, end, utc) { var dates = [ Ox.parseDate(start, utc), Ox.parseDate(end, utc) ], part = [ 'FullYear', 'Month', 'Date', 'Hours', 'Minutes', 'Seconds' ][ Ox.compact( /(-?\d+)-?(\d+)?-?(\d+)? ?(\d+)?:?(\d+)?:?(\d+)?/.exec(end) ).length - 2 ]; Ox['set' + part](dates[1], Ox['get' + part](dates[1], utc) + 1, utc); return dates; }; */ //@ Ox.setDate Set the day of a date, optionally UTC // see Ox.setSeconds for source code //@ Ox.setDay Set the weekday of a date, optionally UTC // see Ox.setSeconds for source code //@ Ox.setFullYear Set the year of a date, optionally UTC // see Ox.setSeconds for source code //@ Ox.setHours Set the hours of a date, optionally UTC // see Ox.setSeconds for source code //@ Ox.setMilliseconds Set the milliseconds of a date // see Ox.setSeconds for source code //@ Ox.setMinutes Set the minutes of a date, optionally UTC // see Ox.setSeconds for source code //@ Ox.setMonth Set the month of a date, optionally UTC // see Ox.setSeconds for source code //@ Ox.setSeconds Set the seconds of a date [ 'FullYear', 'Month', 'Date', 'Day', 'Hours', 'Minutes', 'Seconds', 'Milliseconds' ].forEach(function(part) { Ox['get' + part] = function(date, utc) { return Ox.makeDate(date)['get' + (utc ? 'UTC' : '') + part]() } // Ox.setPart(date) modifies date Ox['set' + part] = function(date, num, utc) { return ( Ox.isDate(date) ? date : new Date(date) )['set' + (utc ? 'UTC' : '') + part](num); } });