Date-related operations in JavaScript

Source: Internet
Author: User
Tags london time zone month name time and date

This article translates the JavaScript Tutorial playlist of up master Kudvenkat on YouTube

SOURCE Address here:

https://www.youtube.com/watch?v=PMsVM7rjupU&list=PL6n9fhu94yhUA99nOsJkKXBqokT3MBK0b

Date () is used to make a date object in JavaScript constructor

The following example displays the current date and time on the page

document.write (new Date ());

If date () constructor is used without any parameters, the current date and time are returned. There are two ways to create a Date object that has a specified time and date:

1. Use a date string

var New Date ("January, 1980 11:20:00");d ocument.write (dateOfBirth);

2. use specific numbers to refer to the year, month, day, hour, minute, second, and millisecond . The format is as follows.

var New Date (year, month, day, hours, minutes, seconds, milliseconds);

Example:

var New Date (1980, 0, 0, 0);d ocument.write (dateOfBirth);

Note: In JavaScript, the number of months is starting from 0, so if you want to get March. Then you should use 2 instead of 3.

The above code will produce the following results on my computer because my computer is using the (UTC) Dublin,edinburgh,lisbon,london time zone

Sun Jan 1980 11:20:00 gmt+0000 (GMT Standard Time)

If you are using a different time zone on your computer, you may get a slightly more varied result. For example, if you are using the (utc+05:30) Chennai, Kolkata, Mumbai, and New Delhi time zone, the results will look like this

Sun Jan 1980 11:20:00 gmt+0530 (India Standard Time)

Some of the more practical methods of date objects

getFullYear ()

Returns the entire year (all 4 numbers will be displayed)

Example: The following example will return 1980

var New Date (1980, 0, 0, 0). getFullYear ();d ocument.write (year);
GetMonth ()

This method returns the number of months (from 0 to 11)

Example: The following example returns 0 (January)

var New Date (1980, 0, 0, 0). GetMonth ();d ocument.write (month);

You can also use the following code to get the month name according to the number of months, the following example will return January

function Getmonthnamefromnumber (monthnumber) {    var monthNames = ["January", "February", "March", "April" ,                        " May "," June "," July "," August "," September ",                         " October "," November "," December ";     return monthnames[monthnumber];} var monthName = Getmonthnamefromnumber (new Date (1980, 0, 0, 0). GetMonth ()); document.write (monthName);
GetDate ()

This method returns the days of the month (from 1 to 31)

Example: The following example returns 13

var New Date (1980, 0, 0, 0). GetDate ();d ocument.write (dayofmonth);
GetDay ()

Returns the number of days in a week (from 0 to 6), 0 for sunday,1 on behalf of Monday, and so on

Example: The following example will return 0

var New Date (1980, 0, 0, 0). GetDay ();d ocument.write (dayOfWeek);

You can use the following code to get the name of a specific day from the day's representative number, and the following code returns Sunday.

function Getweekdaynamefromnumber (daynumber) {    var weekdays = ["Sunday", "Monday", "Tuesday", "Wednesday "," Thursday "," Friday "," Saturday "];     return weekdays[daynumber];} var weekdayname = Getweekdaynamefromnumber (new Date (1980, 0, 0, 0). GetDay ());d ocument . write (weekdayname);

You can also use the following methods to get the part of the date object that gets the time

getHours () // return Hours (0-23) getminutes () // return minutes (0-59) getseconds () // return seconds (0-59) getmilliseconds () // returns milliseconds (0-999)

How do I convert a Date object to a dd/mm/yyyy format?

function formatdate (date) {    var day = date.getdate ();     if (Day [)     {        = ' 0 ' + day ;    }     var month = Date.getmonth () + 1;     if (Month [)     {        = "0" + month;    }     var year = date.getfullyear ();     return Day + "/" + month + "/" + year ;} document.write (formatdate(new Date ()));

If you don't want 0 of the month before the unit, then you can change the code as follows

function formatdate (date) {    var day = date.getdate ();     var month = Date.getmonth () + 1;     var year = date.getfullyear ();     return Day + "/" + month + "/" + year ;} document.write (formatdate(new Date ()));

Date-related operations in JavaScript

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.