Manipulate time and date values with JavaScript date objects A tricky part of software development is the use of time and date values; Each language or platform seems to have its own format. During web development, you can use server-side code to manipulate dates, or you can use JavaScript date objects for the same purpose. In today's column, we'll take a closer look at this object. Use Date Using time and date values in JavaScript is simple. This starts with an instance of the creation Date object: var d = new Date (); This returns an instance of a Date object in variable D using the current date and time. This date object includes several methods and properties for accessing and manipulating its values. The following list lists the methods used to access date values: GetDate (): Returns the date in the month. GetDay (): Returns the day of the week. Starting from Saturday per week (0-6). getFullYear (): Returns a four-digit year. GetMonth (): Returns the month. GetYear (): Returns the double-digit year. getUTCDate (): Returns the date in the month according to Coordinated Universal Time (UTC). getUTCMonth (): Returns the month according to Coordinated Universal Time (0-11). getUTCFullYear (): Returns a four-digit year based on coordinated Universal Time. Note: Wikipedia defines the coordinated world as a high-precision atomic time standard, approximately equivalent to the world time (UT). One thing to note about JavaScript and dates is that it uses the number of milliseconds from midnight January 1, 1970 to save the date. This is called the epoch, and any dates and times before this date are not allowed. As listing a shows, the method of using the previous list is straightforward. You need to note that the date and day of the week values are zero-based, so you need to add a value to them to display their true values. You can easily use an array to display the day of the week. In list B is the JavaScript code. You are not restricted to using only the current date. A Date object can be initialized with the value passed to it, as follows: var d = new Date ("Date value"); Using this method, we can modify the previous example to use a specific date. List C represents a simple way to discover the Sunday period of a given value. The code produces the following result: Today Is:wednesday 4/15/1979 UTC Is:wednesday 4/15/1979 In fact, there are four ways to create a Date object instance: var d = new Date (); var d = new Date (′july 4, 1976′); var d = new Date (7, 4, 1976); var d = new Date (7, 4, 1976, 12,00,00); We've already talked about the top two (note that single quotes or parentheses may be used). The last two uses a separate integer parameter (the time is optional) in the following format: var d = new Date (month, day, year, hour, minutes, seconds); Another way to populate a Date object is to take advantage of the Setdate method. It provides a way to reset the value of a Date object or initialize it, but this requires a true JavaScript Date object: Var D1 = new Date (); var d2 = new Date ("7/4/1976"); D1.setdate (D2.getdate ()); There are many more ways to fix the various properties of a Date object, but let's take a look at the time before discussing them. In addition to the date component, the Date object also holds the time information. The following method provides access to time information for a Date object: GetHours (): Returns the hour portion of the time. Getminutes (): Returns the minute part of the time. Getseconds (): Returns the seconds part of the time. Getmilliseconds (): Returns the millisecond portion of the time. GetTime (): Returns the number of milliseconds since midnight January 1, 1970. getTimezoneOffset (): Returns the minute difference between local time and Galini Standard Time (GMT). getUTCHours (): The hourly portion of the time that is returned according to Coordinated Universal Time. getUTCMinutes (): The minute part of the time that is returned according to Coordinated Universal Time. getUTCSeconds (): The second part of the time that is returned according to Coordinated Universal Time. getUTCMilliseconds (): The millisecond portion of the time that is returned according to Coordinated Universal Time. As mentioned earlier, you can initialize a date object by passing hours, minutes, and seconds, but the millisecond property is set by the Setmilliseconds method. The following JavaScript code shows the current time: <script language= "JavaScript" > var d = new Date (); document.write (d.gethours () + ":" + d.getminutes () + ":" + d.getseconds () + ":" + d.getmilliseconds ()); document.write (D.gettime ()); </script> It will display the following output: 12:36:33:41 1146760593041 The second value is a bit odd because it shows the number of milliseconds since midnight of January 1, 1970 to the value stored in the referenced date object. This is useful when looking for a difference between two values. For date values, there is also a settime method that can be used: Var dt1 = new Date (); var dt2 = new Date (1970, 4, 15); Dt1.settime (Dt2.gettime ()); Setting properties Just like the settime, Setdate, and Setmilliseconds methods, there are ways to populate all parts of a Date object. This includes the following: setFullYear Sethours Setminutes Setmilliseconds Setmonth Setseconds setUTCFullYear setUTCMonth setUTCHours setUTCSeconds setUTCMilliseconds These methods make it easy for you to reset the date property by passing a new value. It's good to be able to use and display dates, but sometimes you need to calculate dates and times. The simplest calculation is the addition and subtraction of two numbers (which you might disagree with), so it's easy to find the difference between the two JavaScript date values. You just have to find the difference and return it as a number. The result is a date value in milliseconds, so you have to divide to get the type of value you want (day, month, minute, hour, and so on). The following JavaScript code is used to calculate the number of days to a date. It subtracts two date values (via GetTime), and then uses the resulting result in milliseconds (86400000) in a day, and finally gets the number of days: <script type= "Text/javascript" > var D1 = new Date (); var d2 = new Date (2006, 6, 7); var day = 1000*60*60*24; var diff = Math.ceil ((D2.gettime ()-d1.gettime ())/(day)); document.write ("Days until Vacation:" + diff); </script> Calculation of dates The various properties of date values can be increased or decreased by using the corresponding attributes to add and subtract the desired values. For example, if you want to increase the value by one months, then you need to add one to the month value. The example in List D shows the difference between yesterday and today in the previous script. Here is the result of the output: Days until Vacation:50 Tomorrow it'll be until vacation. Yesterday, it was until vacation. It's time The use of date and time values has its own limitations, which are different depending on the platform, and web development is not different. JavaScript's date objects provide a simple way to use date and time values, but there are still some things to keep in mind, such as the numbering of seven days and months of the week, and the format of some methods. Once you get used to it, it's not hard to remember. One important thing to keep in mind is that the accuracy of a date or event depends on the clock on the computer where the page is viewed. |