Date Methods

//Define Date to display in local syntax
now = new Date();
LocalTime = now.toLocaleString();

Before you can do anything which involves the current date/time, you have to set a variable equal to the current date. now = new Date() is command to get the current date and time from the user's computer, and assign it to the variable, now in one long string.

toLocaleString() is a method which converts the raw date/time string of text into the local convention. Depending on what country JavaScript thinks the user is in, the toLocaleString() may present the month first, or the day first.

Here are some other Date methods:

//If now = Fri Apr 07 2017 10: 24: 45 GMT + 0700(WIB)

now.getDay() = 5 //Day of week(0 = Sunday)
now.getMonth() = 3 //Month(0 to 11)
now.getDate() = 7 //Day of month
now.getYear() = 117 //Year

//Note: IE returns a 4 digit year, Netscape returns the number of years since 1900. Use this syntax:

year = getYear();
if (year < 1900) {
    year += 1900
};

now.getHours() = 10 //Hours
now.getMinutes() = 24 //Minutes
now.getSeconds() = 45 //Seconds
now.getTime() = 1491535485453 //Milliseconds since January 1, 1990 at Midnight.
now.gettimezoneOffset() = -420 //minutes off from GMT

results matching ""

    No results matching ""