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, and you can also use JavaScript Dates ( Date ) object 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 a double-digit year. getUTCDate (): According to Coordinated Universal Time (UTCreturns the date in the month. getUTCMonth (): According to Coordinated Universal Time (0- One) returns the month. getUTCFullYear (): Returns a four-digit year based on coordinated Universal Time. Note: Wikipedia (Wikipediadefines the coordinated universal time as a high-precision atomic time standard that is approximately equivalent to the world time (UT). One thing to note about JavaScript and dates is that it uses from 1970 year 1 months 1 The number of milliseconds since midnight of the day to save the date. This is calledtheepoch, 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 since1970years1Month1the number of milliseconds since midnight of the day. getTimezoneOffset (): Returns the local time and Galini standard Time (GMTminute difference between the. getUTCHours (): The hourly portion of the time that is returned according to Coordinated Universal Time. getUTCMinutes (): The minute portion 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 passedsetmillisecondsmethod to set the. The following paragraphJavaScriptThe code displays 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 1970 1 months , 1 days, and midnight 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 results in milliseconds (86400000) for 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. |