Click here to Skip to main content
12,952,792 members (35,468 online)
Click here to Skip to main content
Add your own
alternative version

Stats

62.8K views
1.2K downloads
29 bookmarked
Posted 3 Mar 2010

JavaScript DateTime and TimeSpan Wrappers

, 3 Mar 2010 CPOL
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

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)

Share

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

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 5 Pin
Vladosha20-Jan-16 21:19
memberVladosha20-Jan-16 21:19 
QuestionDateTime.now() Pin
Member 1109290110-Mar-15 7:56
memberMember 1109290110-Mar-15 7:56 
GeneralWorks Perfectly! Pin
Member 1109290128-Jan-15 9:46
memberMember 1109290128-Jan-15 9:46 
QuestionConvert TimeStamp String to your TimeStamp class Pin
Rungta Atul9-Nov-12 17:20
memberRungta Atul9-Nov-12 17:20 
GeneralOne issue Pin
Helbrax3-Mar-10 7:02
memberHelbrax3-Mar-10 7:02 
QuestionRe: One issue Pin
Jose Menendez Póo3-Mar-10 7:55
mvpJose Menendez Póo3-Mar-10 7:55 
AnswerRe: One issue Pin
Helbrax3-Mar-10 8:44
memberHelbrax3-Mar-10 8:44 
GeneralRe: One issue Pin
Jose Menendez Póo3-Mar-10 8:33
mvpJose Menendez Póo3-Mar-10 8:33 
GeneralRe: One issue Pin
Helbrax3-Mar-10 8:44
memberHelbrax3-Mar-10 8:44 
GeneralRe: One issue Pin
Josué Molina13-May-14 11:53
memberJosué Molina13-May-14 11:53 

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

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

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170525.1 | Last Updated 3 Mar 2010
Article Copyright 2010 by Jose Menendez Póo
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid