dateutils

fromDateTime

method
DateTime.fromDateTime()

Option name Type Description
year
month

1-12

day

1-31

hours

0-23

minutes

0-59

seconds

0-59

Returns DateTime for given date and time

DateTime.fromDateTime = function(year, month, day, hours, minutes, seconds) {
  return new DateTime(createSafeDate(+year, +month, +day, +hours, +minutes, +seconds || 0))
}

fromDate

method
DateTime.fromDate()

Option name Type Description
year
month
day

Returns DateTime for given date by setting time to midnight

DateTime.fromDate = function(year, month, day) { return DateTime.fromDateTime(year, month, day, 0, 0, 0) }

fromDateObject

method
DateTime.fromDateObject()

Option name Type Description
date

Returns DateTime from given Date object

DateTime.fromDateObject = function(date) { return new DateTime(date) }

fromIsoDate

method
DateTime.fromIsoDate()

Option name Type Description
isoDate

String YYYY-MM-DDTHH-MM

return DateTime

Returns DateTime from ISO date ignoring time information

DateTime.fromIsoDate = function(isoDate) {
  var optionalTimePattern = /^\d{4}-[01]\d-[0-3]\d(T[0-2]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z?))?$/
  if(!optionalTimePattern.test(isoDate)) throw Error(isoDate + ' is not valid ISO Date (YYYY-MM-DD or YYYY-MM-DDTHH:MM)')
  var date = parseDate(isoDate.split('T')[0])
  return DateTime.fromDate(date.year, date.month, date.day)
}

fromIsoDateTime

method
DateTime.fromIsoDateTime()

Option name Type Description
isoDateTime

String YYYY-MM-DDTHH-MM

return DateTime

Returns DateTime or throws error for invalid syntax

Returns DateTime with time from ISO date

DateTime.fromIsoDateTime = function(isoDateTime) {
  var fullPatternTest = /\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z?)/
  if(!fullPatternTest.test(isoDateTime)) throw Error(isoDateTime + ' is not valid ISO Date (YYYY-MM-DDTHH:MM)')

  var dateAndTime = isoDateTime.split('T')
  var time = parseTime(dateAndTime.length === 2 && dateAndTime[1])
  var date = parseDate(dateAndTime[0])
  return DateTime.fromDateTime(date.year, date.month, date.day, time.hours, time.minutes, time.seconds)
}

fromMillis

method
DateTime.fromMillis()

Option name Type Description
ms

Returns DateTime from current time in milliseconds

DateTime.fromMillis = function(ms) { return new DateTime(new Date(ms)) }

withResetMS

method
DateTime.prototype.withResetMS()

Returns new DateTime with milliseconds set to 0

DateTime.prototype.withResetMS = function() {
  var newDate = this.clone()
  newDate.date.setMilliseconds(0)
  return newDate
}

withTime

method
DateTime.prototype.withTime()

Option name Type Description
h

0-23

m

0-59

Returns new DateTime with given hours and minutes and 0 milliseconds

DateTime.prototype.withTime = function(h, m) {
  if(typeof h === 'string') {
    var hoursAndMinutes = h.split(':')
    h = hoursAndMinutes[0]
    m = hoursAndMinutes[1]
  }
  var dateWithTime = this.clone()
  dateWithTime.date.setHours(h)
  dateWithTime.date.setMinutes(m)
  dateWithTime.date.setSeconds(0)
  dateWithTime.date.setMilliseconds(0)
  return dateWithTime
}

DateTime.SECOND = 1000
DateTime.MINUTE = 60 * DateTime.SECOND
DateTime.HOUR = 60 * DateTime.MINUTE
DateTime.DAY = 24 * DateTime.HOUR
DateTime.WEEK = 7 * DateTime.DAY

now

method
DateTime.now()

Returns new DateTime with current time

DateTime.now = function() { return new DateTime() }

today

method
DateTime.today()

Returns new DateTime with current date and midnight time

DateTime.today = function() { return DateTime.now().getOnlyDate() }

getTime

method
DateTime.prototype.getTime()

Returns time in milliseconds

DateTime.prototype.getTime = function() { return this.date.getTime() }

getFullYear

method
DateTime.prototype.getFullYear()

Returns year

DateTime.prototype.getFullYear = function() { return this.date.getFullYear() }

getDate

method
DateTime.prototype.getDate()

Returns day of month

DateTime.prototype.getDate = function() { return this.date.getDate() }

getMonth

method
DateTime.prototype.getMonth()

Returns month

DateTime.prototype.getMonth = function() { return this.date.getMonth() + 1 }

getDay

method
DateTime.prototype.getDay()

Returns day of week. 0=sunday, 1=monday, ...

DateTime.prototype.getDay = function() { return this.date.getDay() }

getHours

method
DateTime.prototype.getHours()

Returns hours

DateTime.prototype.getHours = function() { return this.date.getHours() }

getMinutes

method
DateTime.prototype.getMinutes()

Returns minutes

DateTime.prototype.getMinutes = function() { return this.date.getMinutes() }

getSeconds

method
DateTime.prototype.getSeconds()

Returns seconds

DateTime.prototype.getSeconds = function() { return this.date.getSeconds() }

getMilliseconds

method
DateTime.prototype.getMilliseconds()

Returns milliseconds

DateTime.prototype.getMilliseconds = function() { return this.date.getMilliseconds() }

getDaysInMonth

method
DateTime.prototype.getDaysInMonth()

Returns days in month for current DateTime

DateTime.prototype.getDaysInMonth = function() { return DateTime.getDaysInMonth(this.getFullYear(), this.getMonth()) }

getDayInYear

method
DateTime.prototype.getDayInYear()

Returns days in year for current Date

DateTime.prototype.getDayInYear = function() { return DateTime.getDayInYear(this.getFullYear(), this.getMonth(), this.getDate()) }

plusDays

method
DateTime.prototype.plusDays()

Option name Type Description
days

Returns new DateTime with given days later

DateTime.prototype.plusDays = function(days) {
  var newDateTime = DateTime.fromMillis(this.getTime() + days * DateTime.DAY)
  var hours = this.getHours()

  // Fix the DateTime offset caused by daylight saving time
  var delta = hours - newDateTime.getHours()
  if(delta !== 0) {
    // Correct the delta to be between [-12, 12]
    if(delta > 12) {
      delta -= 24
    }
    if(delta < -12) {
      delta += 24
    }
    return DateTime.fromMillis(newDateTime.getTime() + (delta * DateTime.HOUR))
  }
  return newDateTime
}

plusMinutes

method
DateTime.prototype.plusMinutes()

Option name Type Description
minutes

Returns new DateTime with given minutes later

DateTime.prototype.plusMinutes = function(minutes) { return DateTime.fromMillis(this.clone().getTime() + (minutes * DateTime.MINUTE)) }

minusMinutes

method
DateTime.prototype.minusMinutes()

Option name Type Description
minutes

Returns new DateTime with given minutes earlier

DateTime.prototype.minusMinutes = function(minutes) { return this.plusMinutes(-minutes) }

minusDays

method
DateTime.prototype.minusDays()

Option name Type Description
days

Returns new DateTime with given days earlier

DateTime.prototype.minusDays = function(days) { return this.plusDays(-days) }

compareTo

method
DateTime.prototype.compareTo()

Option name Type Description
date

{DateTime}

Compares DateTimes. Examples:
earlier.compareTo(later)) < 0
later.compareTo(earlier)) > 0
later.compareTo(later)) == 0

DateTime.prototype.compareTo = function(date) {
  if(!date) {
    return 1
  }
  var diff = this.getTime() - date.getTime()
  return diff === 0 ? 0 : diff / Math.abs(diff)
}

isToday

method
DateTime.prototype.isToday()

Returns true if DateTime is within today

DateTime.prototype.isToday = function() { return this.equalsOnlyDate(DateTime.today()) }

getWeekInYear

method
DateTime.prototype.getWeekInYear()

Option name Type Description
weekNumberingSystem string

US or ISO

Returns the week number of current DateTime

DateTime.prototype.getWeekInYear = function(weekNumberingSystem) {
  if(weekNumberingSystem !== 'US' && weekNumberingSystem !== 'ISO') {
    throw('Week numbering system must be either US or ISO, was ' + weekNumberingSystem)
  }

  var firstDay = new Date(this.getFullYear(), 0, 1).getDay()
  if(weekNumberingSystem === 'US') {
    return Math.ceil((this.getDayInYear() + firstDay) / 7)
  }
  var THU = 4
  var weekday = this.getDay()
  if(weekday === 0) weekday = 7
  if(firstDay === 0) firstDay = 7
  // If Dec 29 falls on Mon, Dec 30 on Mon or Tue, Dec 31 on Mon - Wed, it's on the first week of next year
  if(this.getMonth() === 12 && this.getDate() >= 29 && (this.getDate() - weekday) > 27) {
    return 1
  }
  // If Jan 1-3 falls on Fri, Sat or Sun, it's on the last week of the previous year
  if(this.getMonth() === 1 && this.getDate() < 4 && weekday > THU) {
    return new DateTime(new Date(this.getFullYear() - 1, 11, 31)).getWeekInYear('ISO')
  }
  var week = Math.ceil((this.getDayInYear() + firstDay - 1) / 7)
  // If first days of this year are on last year's last week, the above gives one week too much
  if(firstDay > THU) week--
  return week
}

clone

method
DateTime.prototype.clone()

Creates clone of current DateTime

DateTime.prototype.clone = function() { return new DateTime(this.date) }

isOddMonth

method
DateTime.prototype.isOddMonth()

Returs true if month is odd, ie. january=true

DateTime.prototype.isOddMonth = function() { return this.getMonth() % 2 === 0 }

equalsOnlyDate

method
DateTime.prototype.equalsOnlyDate()

Option name Type Description
date

Returns true if given DateTime has same day as current DateTime

DateTime.prototype.equalsOnlyDate = function(date) {
  if(!date) return false
  return this.getMonth() === date.getMonth() && this.getDate() === date.getDate() && this.getFullYear() === date.getFullYear()
}

firstDateOfMonth

method
DateTime.prototype.firstDateOfMonth()

Returns first date of month from current date

DateTime.prototype.firstDateOfMonth = function() { return DateTime.fromDate(this.getFullYear(), this.getMonth(), 1) }

lastDateOfMonth

method
DateTime.prototype.lastDateOfMonth()

Returns last date of month from current date

DateTime.prototype.lastDateOfMonth = function() { return DateTime.fromDate(this.getFullYear(), this.getMonth(), this.getDaysInMonth()) }

distanceInDays

method
DateTime.prototype.distanceInDays()

Option name Type Description
date

Returns number of days between current and given date

DateTime.prototype.distanceInDays = function(date) {
  var first = parseInt(this.getTime() / DateTime.DAY, 10)
  var last = parseInt(date.getTime() / DateTime.DAY, 10)
  return (last - first)
}

withWeekday

method
DateTime.prototype.withWeekday()

Option name Type Description
weekday

0=sunday, 1=monday, ...

Returns new DateTime from same week with given weekDay

DateTime.prototype.withWeekday = function(weekday) { return this.plusDays(weekday - this.getDay()) }

getOnlyDate

method
DateTime.prototype.getOnlyDate()

Returns new DateTime with midnight time

DateTime.prototype.getOnlyDate = function() { return DateTime.fromDate(this.getFullYear(), this.getMonth(), this.getDate()) }

isWeekend

method
DateTime.prototype.isWeekend()

Returns true if date is in weekend

DateTime.prototype.isWeekend = function() { return this.getDay() === 6 || this.getDay() === 0 }

toString

method
DateTime.prototype.toString()

Returns default string representation

DateTime.prototype.toString = function() { return this.toISOString() }

getFirstDateOfWeek

method
DateTime.prototype.getFirstDateOfWeek()

Option name Type Description
locale

Based on locale it can be a monday or a sunday

Returns first date from same week

DateTime.prototype.getFirstDateOfWeek = function(locale) {
  var firstWeekday = locale ? locale.firstWeekday : DateTime.MONDAY
  if(firstWeekday == this.getDay) return this.clone()
  else return this.plusDays(firstWeekday - this.getDay() - (firstWeekday > this.getDay() ? 7 : 0))
}

toISOString

method
DateTime.prototype.toISOString()

Returns ISO DateTime string: YYYY-MM-DDT:HH:MM:SS

DateTime.prototype.toISOString = function() { return isoDate.call(this) + 'T' + isoTime.call(this) }

toISODateString

method
DateTime.prototype.toISODateString()

Returns ISO Date string: YYYY-MM-DD

DateTime.prototype.toISODateString = function() { return isoDate.call(this) }

isBetweenDates

method
DateTime.prototype.isBetweenDates()

Option name Type Description
start DateTime
end DateTime

Returns true if current DateTime is between start and end DateTimes

DateTime.prototype.isBetweenDates = function(start, end) {
  if(start.getTime() > end.getTime()) throw Error("start date can't be after end date")
  var onlyDate = this.getOnlyDate()
  return onlyDate.compareTo(start.getOnlyDate()) >= 0 && onlyDate.compareTo(end.getOnlyDate()) <= 0
}

getDaysInMonth

method
DateTime.getDaysInMonth()

Option name Type Description
year Number

Year of month

month Number

Number of month (1-12)

Returns number of days for given month

DateTime.getDaysInMonth = function(year, month) {
  if(month > 12 || month < 1)
    throw new Error('Month must be between 1-12')
  var yearAndMonth = year * 12 + month
  return DateTime.fromDate(Math.floor(yearAndMonth / 12), yearAndMonth % 12 + 1, 1).minusDays(1).getDate()
}

getDayInYear

method
DateTime.getDayInYear()

Option name Type Description
year

year

month

month

day

day

Returns index of given day from beginning of year

DateTime.getDayInYear = function(year, month, day) {
  return DateTime.fromDate(year, 1, 1).distanceInDays(DateTime.fromDate(year, month, day)) + 1
}

function isoDate() { return this.getFullYear() + '-' + twoDigits(this.getMonth()) + '-' + twoDigits(this.getDate()) }

function isoTime() {
  return twoDigits(this.getHours()) + ':' + twoDigits(this.getMinutes()) + ':' + twoDigits(this.getSeconds())
}

function twoDigits(value) { return value < 10 ? '0' + value : '' + value}

function createSafeDate(year, month, date, hours, minutes, seconds) {
  hours = hours || 0
  minutes = minutes || 0
  seconds = seconds || 0
  var newDate = new Date(year, month - 1, date, hours, minutes, seconds, 0)
  if(newDate.toString() === 'Invalid Date' ||
    month !== newDate.getMonth() + 1 ||
    year !== newDate.getFullYear() ||
    date !== newDate.getDate() ||
    hours !== newDate.getHours() ||
    minutes !== newDate.getMinutes() ||
    seconds !== newDate.getSeconds()) throw Error('Invalid Date: ' + year + '-' + month + '-' + date + ' ' + hours + ':' + minutes + ':' + seconds)
  return newDate
}

function parseDate(str) {
  var dateComponents = str.split('-')
  return {
    year:  +dateComponents[0],
    month: +dateComponents[1],
    day:   +dateComponents[2]
  }
}

function parseTime(str) {
  if(str) {
    var timeComponents = str.split(':')
    return {
      hours:   +timeComponents[0],
      minutes: +timeComponents[1],
      seconds: +timeComponents[2] || 0
    }
  } else {
    return {hours: 0, minutes: 0}
  }
}

module.exports = DateTime