|Written by Ian Elliot|
|Thursday, 15 June 2017|
Page 2 of 2
Date and times as strings
The most basic of these is the toString method which returns a string showing the date and local time with the correction applied to the UTC value:
This is complete but often more than is required by an innocent user who has no idea what UTC is!
A more user friendly version of this method is the toLocaleString method which shows the local time and date in the local format without any UTC information:
Of course if you want the UTC date and time then you can use the toUTCString method
If you just want the date and not the time then you can use the toDateString method or the toLocaleDateString method which returns the date formatted in the local style:
Similarly if you only want the time part of the date and time you can use the toTimeString method which returns a local time with an indication of the UTC correction applied or the toLocaleTimeString which returns the local time without a mention of UTC:
The one that is missing is a method to return a UTC time but this isn't a big loss and it can be made up for very easily.
Extending date formats
What do you do if you want a date or time in a different format?
You have two possible solutions.
You could use the raw date and time access methods such as getHours and put together the date and time format you want. It also makes sense to extend the methods of the Date instances by adding methods to the prototype. For example to construct the day name followed by the day number you could write:
and then write:
to display Mon 5.
The alternative way is to take the existing string methods and process their results. For example the toString method has all of the information we need to implement the toDayDate method by extracting the information from a string something like:
So all we need are the first three characters and the two starting at the eighth:
This works just as well as the previous method.
It is up to you which you choose.
Now we come to perhaps the most confusing part of using the Date object - date arithmetic.
Let's consider a particular problem.
You want to add one day to today's date and time to get the date and time of tomorrow.
To solve this problem you have to remember that the Date object stores the number of milliseconds from the fixed date. So to add one day you have to add one day's worth of milliseconds i.e. 24*60*60*1000.
However if you try:
you will discover that the result is the date string with the result of the calculation concatenated on the end. Of course when you use a Date object in an expression either the toString or toValue method is called to provide a default string or numeric value. In this case the toString method is called and the expression is interpreted as a string operation. If you want to know more about default values see - Objects with values.
This works because 1*t has to be a number however this isn't a good way to express the idea. Much better is to use the getTime method which returns the number of milliseconds as a numeric value. However if you try:
You will find that instead seeing a date string as the result you see a numeric value. This is perfectly reasonable, after all you converted t to a numeric value and then added something to it. The result is that t2 is numeric and not a Date object.
To get the result as a Date object you have to make a new one based on the number of millisecond ticks:
The only problem with this is that it would be nice to have a general way of working out the millisecond tick value of an arbitrary date or time interval, e.g. one hour, ten days, three minutes and so on.
One way of doing this is to construct a Date object with that number of millisecond ticks away from the fixed date. For example, a Date object that has one hour's worth of milliseconds can be created using:
similarly one minute is:
and one second:
a day is just:
i.e. one day away from the fixed date.
Once you have these intervals you can do date/time arithmetic very easily by using the getTime method to convert to milliseconds and constructing a new Date object when you need to. For example:
You now should be able to see how to generalize this to any date/time arithmetic you need to perform. There is more about date and time arithmetic in the next two chapters.
Of course you could add extension methods to the Date object to provide a more regular way of doing date/time arithmetic but there are advantages to adding your own TimeInterval object type that has these extra methods and doesn't modify the built-in object. In fact this is always a better idea than extending the built-in objects and this is exactly what the next chapter is all about.
or email your comment to: firstname.lastname@example.org