JavaScript Date Methods

JavaScript Date Methods

JavaScript Date Methods

Date methods let you get and set date values (years, months, days, hours, minutes, seconds, milliseconds)

Date Get Methods

Get methods are used for getting a part of a date. Here are the most common (alphabetically):

The getTime() Method

getTime() comebacks the number of milliseconds since January 1, 1970:

Example

var d = fresh Date(),

The getFullYear() Method

getFullYear() comebacks the year of a date as a four digit number:

Example

var d = fresh Date(),

The getDay() Method

getDay() comes back the weekday as a number (0-6):

Example

var d = fresh Date(),

In JavaScript, the very first day of the week (0) means ",Sunday",, even if some countries in the world consider the very first day of the week to be ",Monday",

You can use an array of names, and getDay() to comeback the weekday as a name:

Example

var d = fresh Date(),

Date Set Methods

Set methods are used for setting a part of a date. Here are the most common (alphabetically):

The setFullYear() Method

setFullYear() sets a date object to a specific date. In this example, to January 14, 2020:

Example

var d = fresh Date(),

d.setFullYear(2020, 0, 14),

The setDate() Method

setDate() sets the day of the month (1-31):

Example

var d = fresh Date(),

The setDate() method can also be used to add days to a date:

Example

var d = fresh Date(),

If adding days, shifts the month or year, the switches are treated automatically by the Date object.

Date Input – Parsing Dates

If you have a valid date string, you can use the Date.parse() method to convert it to milliseconds.

Date.parse() comebacks the number of milliseconds inbetween the date and January 1, 1970:

Example

var msec = Date.parse(",March 21, 2012",),

You can then use the number of milliseconds to convert it to a date object:

Example

var msec = Date.parse(",March 21, 2012",),

var d = fresh Date(msec),

Compare Dates

Dates can lightly be compared.

The following example compares today’s date with January 14, 2100:

Example

today = fresh Date(),

someday = fresh Date(),

someday.setFullYear(2100, 0, 14),

text = ",Today is before January 14, 2100.",,

text = ",Today is after January 14, 2100.",,

JavaScript counts months from 0 to 11. January is 0. December is 11.

UTC Date Methods

UTC date methods are used for working UTC dates (Universal Time Zone dates):

Accomplish JavaScript Date Reference

The reference contains descriptions and examples of all Date properties and methods.

COLOR PICKER

HOW TO

SHARE

CERTIFICATES

HTML, CSS, JavaScript, PHP, jQuery, Bootstrap and XML.

Related movie: Dating site for born again Christians – free!!


Leave a Reply

Your email address will not be published. Required fields are marked *