Click here to Skip to main content
Click here to Skip to main content

JavaScript DateTime and TimeSpan Wrappers

, 3 Mar 2010
Rate this:
Please Sign up or sign in to vote.
A couple of JavaScript classes with the well known .NET's classes capabilities

Introduction

Imagine using this code in JavaScript:

<script type="text/javascript">

    //Get the current date and time stamp.
    var date = DateTime.now();

    //Add 100 hours to the date
    date = date.addHours(100); 

    //Write the date in a formatted fashion: Monday, 15/Feb/2012
    document.writeline("dddd, dd/MMM/yyyy");

</script> 

Yes, the usage is very similar to .NET, but it's pure JavaScript.

I have always liked the way .NET's DateTime and TimeSpan classes are designed. I find them very useful and intuitive. These couple of classes are designed to work in a very similar manner.

The project is hosted at: http://sourceforge.net/projects/jsdatetime.

DateTime Class

Constructor Description
DateTime(millseconds : int) Creates a DateTime starting from the specified amount of millseconds.
DateTime(year : int, month : int, day : int) Creates a DateTime for the specified date elements
DateTime(year : int, month : int, day : int, hour : int, minute : int, second : int) Creates a DateTime 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 result TimeSpan.
subtractTime(t : TimeSpan) DateTime Subtracts the specified TimeSpan and returns the result DateTime.
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.

Hope it helps.

History

  • 3/Mar/2010 Original post
  • 3/Mar/2010 1 hour later... Fix of "memory leak"

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Jose Menendez Póo
Team Leader
Mexico Mexico
I'm in game programming now: https://itunes.apple.com/us/app/ugly-aliens-training-center/id859271884?ls=1&mt=8
 
Jose Manuel Menéndez Poó
 
- I've been programming Windows and Web apps since 1997.
- My greatest concern nowadays is user interface usability.
 
Questions and stuff by twitter: @menendezpoo
 
Blog
menendezpoo.com

Comments and Discussions

 
QuestionConvert TimeStamp String to your TimeStamp class PinmemberRungta Atul9-Nov-12 17:20 
GeneralOne issue PinmemberHelbrax3-Mar-10 7:02 
QuestionRe: One issue PinmvpJose Menendez Póo3-Mar-10 7:55 
AnswerRe: One issue PinmemberHelbrax3-Mar-10 8:44 
GeneralRe: One issue PinmvpJose Menendez Póo3-Mar-10 8:33 
GeneralRe: One issue PinmemberHelbrax3-Mar-10 8:44 
GeneralRe: One issue [modified] PinmemberJosué Molina13-May-14 11:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140718.1 | Last Updated 3 Mar 2010
Article Copyright 2010 by Jose Menendez Póo
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid