Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Mobile / WinMobile

JavaScript DateTime and TimeSpan Wrappers

4.82/5 (12 votes)
3 Mar 2010CPOL6 min read 1   1.5K  
A couple of JavaScript classes with the well known .NET's classes capabilities

Introduction

Imagine using this code in JavaScript:

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

ConstructorDescription
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 MethodReturn ValueDescription
DateTime.daysInMonth(year : int, month : int)intReturns the days of the specified month for the specified year.
DateTime.isLeapYear(year : int)boolReturns a boolean indicating if the specified year is leap year (February has 29 days)
DateTime.now()DateTimeReturns the current date and time.
DateTime.today()DateTimeReturns the current date, with zero time.
DateTime.utcNow()DateTimeReturns the UTC (Previously called GMT) time.
MethodReturn ValueDescription
add(time : TimeSpan)DateTimeAdds 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)DateTimeAdds the specified number of milliseconds to the date and returns the result.
addMinutes(minutes: int)DateTimeAdds the specified number of minutes to the date and returns the result.
addMonths(months : int)DateTimeAdds the specified number of months to the date and returns the result.
addSeconds(seconds : int)DateTimeAdds the specified number of seconds to the date and returns the result.
addYears(years : int)DateTimeAdds the specified number of years to the date and returns the result.
compareTo(d : DateTime)intReturns -1, 0 or 1, depending on the compare result.
date()DateTimeReturns just the date, with no time.
day()intReturns the day of the date.
dayOfWeek()intReturns the day of week. 1 = Monday to 7 = Sunday
dayOfYear()intReturns the day of year
equals(d : DateTime)boolReturns 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()intGets the hour of the date.
millisecond()intGets the millisecond of the date.
minute()intGets the minute of the date.
month()intGets the month of the date.
second()intGets the second of the date.
subtractDate(d : DateTime)TimeSpanSubtracts the specified DateTime and returns the result TimeSpan.
subtractTime(t : TimeSpan)DateTimeSubtracts the specified TimeSpan and returns the result DateTime.
timeOfDay()TimeSpanGets just the time part of the date.
year()intGets the year of the date.

Translation Strings

You may translate the strings of the class by changing the values of the following static properties:

PropertyDefault Value
DateTme.strings.MonMon
DateTme.strings.MondayMonday
DateTme.strings.TueTue
DateTme.strings.TuesdayTuesday
DateTme.strings.Wed Wed
DateTme.strings.WednesdayWednesday
DateTme.strings.ThuThu
DateTme.strings.ThursdayThursday
DateTme.strings.FriFri
DateTme.strings.FridayFriday
DateTme.strings.SatSat
DateTme.strings.SaturdaySaturday
DateTme.strings.SunSun
DateTme.strings.SundaySunday
DateTme.strings.JanJan
DateTme.strings.JanuaryJanuary
DateTme.strings.FebFeb
DateTme.strings.FebruaryFebruary
DateTme.strings.MarMar
DateTme.strings.MarchMarch
DateTme.strings.AprApr
DateTme.strings.AprilApril
DateTme.strings.MayMay
DateTme.strings.MayFullMay
DateTme.strings.JunJun
DateTme.strings.JuneJune
DateTme.strings.JulJul
DateTme.strings.JulyJuly
DateTme.strings.AugAug
DateTme.strings.AugustAugust
DateTme.strings.SepSep
DateTme.strings.SeptemberSeptember
DateTme.strings.OctOct
DateTme.strings.OctoberOctober
DateTme.strings.NovNov
DateTme.strings.NovemberNovember
DateTme.strings.DecDec
DateTme.strings.DecemberDecember
DateTme.strings.AA
DateTme.strings.AMAM
DateTme.strings.PP
DateTme.strings.PMPM
DateTme.strings.TimeSeparator:
DateTme.strings.DateSeparator/

TimeSpan Class

ConstructorDescription
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.
MethodReturn ValueDescription
add(t : TimeSpan)TimeSpanAdds the specified time to the timespan and returns the result.
compareTo(t : TimeSpan)int-1, 0 or 1, depending on the compare result.
days()intReturns the days in the TimeSpan.
duration()TimeSpanReturns the absolute value of the TimeSpan.
equals(t : TimeSpan)boolReturns a bool indicating if the specified TimeSpan has the same value.
hours()intReturns the hours in the TimeSpan.
negate()TimeSpanReturns the negation of the time.
milliseconds()intReturns the milliseconds in the TimeSpan.
minutes()intReturns the minutes in the TimeSpan.
seconds()intReturns the seconds int the TimeSpan.
totalDays()doubleReturns the total days in the TimeSpan.
totalHours()doubleReturns the total hours in the TimeSpan.
totalMinutes()doubleReturns the total minutes in the TimeSpan.
totalMilliseconds()intReturns the total of milliseconds in the TimeSpan.
totalSeconds()doubleReturns the total of seconds in the TimeSpan.
subtract(t : TimeSpan)TimeSpanSubtracts 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)