Set year of Date javascript

JavaScript provides Date object to work with date & time, including days, months, years, hours, minutes, seconds, and milliseconds.

Use the

new Date()
new Date(value)
new Date(dateString)
new Date(year, monthIndex)
new Date(year, monthIndex, day)
new Date(year, monthIndex, day, hours)
new Date(year, monthIndex, day, hours, minutes)
new Date(year, monthIndex, day, hours, minutes, seconds)
new Date(year, monthIndex, day, hours, minutes, seconds, milliseconds)
0 function to get the string representation of the current date and time in JavaScript. Use the new keyword in JavaScript to get the Date object.

Example: Date In JavaScript


Date(); //Returns current date and time string

//or

var currentDate = new Date(); //returns date object of current date and time

Try it

Create a date object by specifying different parameters in the

new Date()
new Date(value)
new Date(dateString)
new Date(year, monthIndex)
new Date(year, monthIndex, day)
new Date(year, monthIndex, day, hours)
new Date(year, monthIndex, day, hours, minutes)
new Date(year, monthIndex, day, hours, minutes, seconds)
new Date(year, monthIndex, day, hours, minutes, seconds, milliseconds)
0 constructor function.

Date() Syntax

new Date()
new Date(value)
new Date(dateString)
new Date(year, monthIndex)
new Date(year, monthIndex, day)
new Date(year, monthIndex, day, hours)
new Date(year, monthIndex, day, hours, minutes)
new Date(year, monthIndex, day, hours, minutes, seconds)
new Date(year, monthIndex, day, hours, minutes, seconds, milliseconds)

Parameters:

  • No Parameters: A date object will be set to the current date & time if no parameter is specified in the constructor.
  • value: An integer value representing the number of milliseconds since January 1, 1970, 00:00:00 UTC.
  • dateString: A string value that will be parsed using
    new Date()
    new Date(value)
    new Date(dateString)
    new Date(year, monthIndex)
    new Date(year, monthIndex, day)
    new Date(year, monthIndex, day, hours)
    new Date(year, monthIndex, day, hours, minutes)
    new Date(year, monthIndex, day, hours, minutes, seconds)
    new Date(year, monthIndex, day, hours, minutes, seconds, milliseconds)
    
    2 method.
  • year: An integer value to represent a year of a date. Numbers from 0 to 99 map to the years 1900 to 1999. All others are actual years.
  • monthIndex: An integer value to represent a month of a date. It starts with 0 for January till 11 for December
  • day: An integer value to represent day of the month.
  • hours: An integer value to represent the hour of a day between 0 to 23.
  • minutes: An integer value to represent the minute of a time segment.
  • seconds: An integer value to represent the second of a time segment.
  • milliseconds: An integer value to represent the millisecond of a time segment. Specify numeric milliseconds in the constructor to get the date and time elapsed from 1/1/1970.

In the following example, a date object is created by passing milliseconds in the

new Date()
new Date(value)
new Date(dateString)
new Date(year, monthIndex)
new Date(year, monthIndex, day)
new Date(year, monthIndex, day, hours)
new Date(year, monthIndex, day, hours, minutes)
new Date(year, monthIndex, day, hours, minutes, seconds)
new Date(year, monthIndex, day, hours, minutes, seconds, milliseconds)
0 constructor function. So date will be calculated based on milliseconds elapsed from 1/1/1970.

Example: Create Date by Specifying Milliseconds

var date1 = new Date(0);  // Thu Jan 01 1970 05:30:00

var date2 = new Date(1000); // Thu Jan 01 1970 05:30:01

var date3 = new Date(5000); // Thu Jan 01 1970 05:30:05 

Try it

The following example shows various formats of a date string that can be specified in a

new Date()
new Date(value)
new Date(dateString)
new Date(year, monthIndex)
new Date(year, monthIndex, day)
new Date(year, monthIndex, day, hours)
new Date(year, monthIndex, day, hours, minutes)
new Date(year, monthIndex, day, hours, minutes, seconds)
new Date(year, monthIndex, day, hours, minutes, seconds, milliseconds)
0 constructor.

Example: Create Date by Specifying Date String

var date1 = new Date("3 march 2015");

var date2 = new Date("3 February, 2015");

var date3 = new Date("3rd February, 2015"); // invalid date

var date4 = new Date("2015 3 February");

var date5 = new Date("3 2015 February ");

var date6 = new Date("February 3 2015");

var date7 = new Date("February 2015 3");

var date8 = new Date("2 3 2015");

var date9 = new Date("3 march 2015 20:21:44");

Try it

You can use any valid separator in the date string to differentiate date segments.

Example: Create Date using Different Date Separator

var date1 = new Date("February 2015-3");

var date2 = new Date("February-2015-3");

var date3 = new Date("February-2015-3");

var date4 = new Date("February,2015-3");

var date5 = new Date("February,2015,3");

var date6 = new Date("February*2015,3");

var date7 = new Date("February$2015$3");

var date8 = new Date("3-2-2015"); // MM-dd-YYYY

var date9 = new Date("3/2/2015"); // MM-dd-YYYY

Try it

Specify seven numeric values to create a date object with the specified year, month and optionally date, hours, minutes, seconds and milliseconds.

Example: Date

var date1 = new Date(2021, 2, 3); // Mon Feb 03 2021 
var date2 = new Date(2021, 2, 3, 10); // Mon Feb 03 2021 10:00 
var date3 = new Date(2021, 2, 3, 10, 30); // Mon Feb 03 2021 10:30 
var date4 = new Date(2021, 2, 3, 10, 30, 50); // Mon Feb 03 2021 10:30:50 
var date5 = new Date(2021, 2, 3, 10, 30, 50, 800); // Mon Feb 03 2021 10:30:50

Try it

Date Formats

JavaScript supports ISO 8601 date format by default -

new Date()
new Date(value)
new Date(dateString)
new Date(year, monthIndex)
new Date(year, monthIndex, day)
new Date(year, monthIndex, day, hours)
new Date(year, monthIndex, day, hours, minutes)
new Date(year, monthIndex, day, hours, minutes, seconds)
new Date(year, monthIndex, day, hours, minutes, seconds, milliseconds)
5

Example: ISO Date Format

var dt = new Date('2015-02-10T10:12:50.5000z');

Convert Date Formats

Use different Date methods to convert a date from one format to another format, e.g., to Universal Time, GMT, or local time format.

The following example demonstrates

new Date()
new Date(value)
new Date(dateString)
new Date(year, monthIndex)
new Date(year, monthIndex, day)
new Date(year, monthIndex, day, hours)
new Date(year, monthIndex, day, hours, minutes)
new Date(year, monthIndex, day, hours, minutes, seconds)
new Date(year, monthIndex, day, hours, minutes, seconds, milliseconds)
6,
new Date()
new Date(value)
new Date(dateString)
new Date(year, monthIndex)
new Date(year, monthIndex, day)
new Date(year, monthIndex, day, hours)
new Date(year, monthIndex, day, hours, minutes)
new Date(year, monthIndex, day, hours, minutes, seconds)
new Date(year, monthIndex, day, hours, minutes, seconds, milliseconds)
7,
new Date()
new Date(value)
new Date(dateString)
new Date(year, monthIndex)
new Date(year, monthIndex, day)
new Date(year, monthIndex, day, hours)
new Date(year, monthIndex, day, hours, minutes)
new Date(year, monthIndex, day, hours, minutes, seconds)
new Date(year, monthIndex, day, hours, minutes, seconds, milliseconds)
8, and
new Date()
new Date(value)
new Date(dateString)
new Date(year, monthIndex)
new Date(year, monthIndex, day)
new Date(year, monthIndex, day, hours)
new Date(year, monthIndex, day, hours, minutes)
new Date(year, monthIndex, day, hours, minutes, seconds)
new Date(year, monthIndex, day, hours, minutes, seconds, milliseconds)
9 methods to convert date into respective formats.

Example: Date Conversion in Different Formats

var date = new Date('2015-02-10T10:12:50.5000z');

date; 'Default format:'

date.toDateString();'Tue Feb 10 2015'

date.toLocaleDateString();'2/10/2015'

date.toGMTString(); 'GMT format' 

date.toISOString(); '2015-02-10T10:12:50.500Z' 

date.toLocaleString();'Local date Format '

date.toLocaleTimeString(); 'Locale time format '

date.toString('YYYY-MM-dd'); 'Tue Feb 10 2015 15:42:50'

date.toTimeString(); '15:42:50' 

date.toUTCString(); 'UTC format ' 

Try it

To get date string in formats other than the ones listed above, you need to manually form the date string using different date object methods. The following example converts a date string to DD-MM-YYYY format.

Example: Get Date Segments

var date = new Date('4-1-2015'); // M-D-YYYY

var d = date.getDate();
var m = date.getMonth() + 1;
var y = date.getFullYear();

var dateString = (d <= 9 ? '0' + d : d) + '-' + (m <= 9 ? '0' + m : m) + '-' + y;

Try it

 

Note:

Use third party JavaScript Date library like datejs.com or momentjs.com to work with Dates extensively in JavaScript.

How to set a year in JavaScript?

setFullYear() sets the year of a date. setFullYear() can also set month and day.

How to get year of a date JavaScript?

getFullYear() returns the full year (4 digits) of a date.

How to update year in JavaScript?

Display current year with JavaScript's getFullYear() method write(new Date(). getFullYear()); Hit enter, and you'll see the current date in the top left corner of your browser window. You can see the code in function inside this CodePen example.

How to set date value in JavaScript?

JavaScript Date setDate().
Example 1. const d = new Date(); d. setDate(15);.
Example 2. Set the day of the month to the last day of the previous month: const d = new Date(); d. setDate(0);.
Example 3. Set the day of the month in a specified date: const d = new Date("July 21, 1983 01:15:00"); d. setDate(15);.