JavaScript DateTime and TimeSpan Wrappers






4.82/5 (10 votes)
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:
|
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"