Working with DateTime in JavaScript

  1. <script type="text/javascript">    
  2.     //Get the current date and time stamp.    
  3.     var date = DateTime.now();    
  4.     //Add 100 hours to the date    
  5.     date = date.addHours(100);    
  6.     //Write the date in a formatted fashion: Monday, 15/Feb/2012    
  7.     document.writeline("dddd, dd/MMM/yyyy");    
  8. </script>   
DateTime Class
 
Constructor Description
DateTime(millseconds : int) Creates a DateTime starting from the specified amount of millseconds
DateTime(year : int, month : int, day : int) For the specified date elements
DateTime(year : int, month : int, day : int, hour : int, minute : int, second : int) For the specified date and time elements
DateTime(year : int, month : int, day : int, hour : int, minute : int, second : int) Creates a DateTime for the specified date and time (with milliseconds) elements
 
Static Method Return Value Description
DateTime.daysInMonth(year : int, month : int) int Returns the days of the specified month for the specified year
DateTime.isLeapYear(year : int) bool Returns a boolean indicating if the specified year is leap year (February has 29 days)
DateTime.now() DateTime Returns the current date and time
DateTime.today() DateTime Returns the current date, with zero time
DateTime.utcNow() DateTime Returns the UTC (Previously called GMT) time
 
Method Return Value Description
add(time : TimeSpan) DateTime Adds the specified time to the date and returns the result
addDays(days : int) DateTime Adds the specified number of days to the date and returns the result
addHours(hours : int) DateTime Adds the specified number of hours to the date and returns the result
addMilliseconds(millis : int) DateTime Adds the specified number of milliseconds to the date and returns the result
addMinutes(minutes: int) DateTime Adds the specified number of minutes to the date and returns the result
addMonths(months : int) DateTime Adds the specified number of months to the date and returns the result
addSeconds(seconds : int) DateTime Adds the specified number of seconds to the date and returns the result
addYears(years : int) DateTime Adds the specified number of years to the date and returns the result
compareTo(d : DateTime) int Returns -1, 0 or 1, depending on the compare result
date() DateTime Returns just the date, with no time
day() int Returns the day of the date
dayOfWeek() int Returns the day of week. 1 = Monday to 7 = Sunday
dayOfYear() int Returns the day of year
equals(d : DateTime) bool Returns a value indicating if d is the same date
format(format : string) string Returns a formatted date. Format is very similar to .NET's date time custom formatting:
  • d - Day of month, 1 to 31
  • dd - Day of Month, 01 to 31
  • ddd - Day of Week, Mon to Sun
  • dddd - Day of Week, Monday to Sunday
  • f - Millisecond / 100
  • ff - Millisecond / 10
  • fff - Millisecond
  • h - Hour of day, 1 to 12
  • hh - Hour of day, 01 to 12
  • H - Hour of day, 1 to 23
  • HH - Hour of day, 01 to 24
  • m - Minute, 1 to 59
  • mm - Minute, 01 to 59
  • M - Month, 1 to 12
  • MM - Month, 01 to 12
  • MMM - Month, Jan to Dec
  • MMMM - Month, January to December
  • s - Second, 1 to 59
  • ss - Second 01 to 59
  • t - A or P
  • tt - AM or PM
  • yy - Year, 10
  • yyyy - Year, 2010
  • : - TimeSeparator
  • / - DateSeparator
hour() int Gets the hour of the date
millisecond() int Gets the millisecond of the date
minute() int Gets the minute of the date
month() int Gets the month of the date
second() int Gets the second of the date
subtractDate(d : DateTime) TimeSpan Subtracts the specified DateTime and returns the resultTimeSpan
subtractTime(t : TimeSpan) DateTime Subtracts the specified TimeSpan and returns the resultDateTime
timeOfDay() TimeSpan Gets just the time part of the date
year() int Gets the year of the date
 
Translation Strings
 
You may translate the strings of the class by changing the values of the following static properties:
 
Property Default Value
DateTme.strings.Mon Mon
DateTme.strings.Monday Monday
DateTme.strings.Tue Tue
DateTme.strings.Tuesday Tuesday
DateTme.strings.Wed Wed
DateTme.strings.Wednesday Wednesday
DateTme.strings.Thu Thu
DateTme.strings.Thursday Thursday
DateTme.strings.Fri Fri
DateTme.strings.Friday Friday
DateTme.strings.Sat Sat
DateTme.strings.Saturday Saturday
DateTme.strings.Sun Sun
DateTme.strings.Sunday Sunday
DateTme.strings.Jan Jan
DateTme.strings.January January
DateTme.strings.Feb Feb
DateTme.strings.February February
DateTme.strings.Mar Mar
DateTme.strings.March March
DateTme.strings.Apr Apr
DateTme.strings.April April
DateTme.strings.May May
DateTme.strings.MayFull May
DateTme.strings.Jun Jun
DateTme.strings.June June
DateTme.strings.Jul Jul
DateTme.strings.July July
DateTme.strings.Aug Aug
DateTme.strings.August August
DateTme.strings.Sep Sep
DateTme.strings.September September
DateTme.strings.Oct Oct
DateTme.strings.October October
DateTme.strings.Nov Nov
DateTme.strings.November November
DateTme.strings.Dec Dec
DateTme.strings.December December
DateTme.strings.A A
DateTme.strings.AM AM
DateTme.strings.P P
DateTme.strings.PM PM
DateTme.strings.TimeSeparator :
DateTme.strings.DateSeparator /
 
TimeSpan Class
 
Constructor Description
TimeSpan(millis : int) Creates a TimeSpan from the specified amount of milliseconds
TimeSpan(days : int, hours : int) Creates a TimeSpan from the specified amount of days and hours
TimeSpan( hours : int, minutes : int, seconds : int) Creates a TimeSpan from the specified amount of hours, minutes and seconds
TimeSpan(days : int, hours : int, minutes : int, seconds : int) Creates a TimeSpan from the specified amount of days, hours, minutes and seconds
TimeSpan(days : int, hours : int, minutes : int, seconds : int, millis : int) Creates a TimeSpan from the specified amount of days, hours, minutes, seconds and milliseconds
 
Method Return Value Description
add(t : TimeSpan) TimeSpan Adds the specified time to the timespan and returns the result
compareTo(t : TimeSpan) int -1, 0 or 1, depending on the compare result
days() int Returns the days in the TimeSpan
duration() TimeSpan Returns the absolute value of the TimeSpan
equals(t : TimeSpan) bool Returns a bool indicating if the specified TimeSpan has the same value
hours() int Returns the hours in the TimeSpan
negate() TimeSpan Returns the negation of the time
milliseconds() int Returns the milliseconds in the TimeSpan
minutes() int Returns the minutes in the TimeSpan
seconds() int Returns the seconds int the TimeSpan
totalDays() double Returns the total days in the TimeSpan
totalHours() double Returns the total hours in the TimeSpan
totalMinutes() double Returns the total minutes in the TimeSpan
totalMilliseconds() int Returns the total of milliseconds in the TimeSpan
totalSeconds() double Returns the total of seconds in the TimeSpan
subtract(t : TimeSpan) TimeSpan Subtracts the specified TimeSpan to the value and returns the result