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).
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);
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.
var d = new Date ();
document.write (d.gethours () + ":" + d.getminutes () + ":" + d.getseconds () +
":" + d.getmilliseconds ());
document.write (D.gettime ());
It will display the following output:
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 ());
Just like the settime, Setdate, and Setmilliseconds methods, there are ways to populate all parts of a Date object. This includes the following:
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.
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);
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.