Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
  • JavaScript - Get Started
  • What is JavaScript
  • Setup JavaScript Dev Environment
  • HTML <script> Tag
  • JavaScript - Syntax
  • JavaScript - Popup Message
  • JavaScript - Variables
  • JavaScript - Operators
  • JavaScript - Data Types
  • JavaScript - String
  • JavaScript - Numbers
  • JavaScript - Boolean
  • JavaScript - Object
  • JavaScript - Date
  • JavaScript - Date Methods
  • JavaScript - Array
  • JavaScript - Array Methods
  • JavaScript - null and undefined
  • JavaScript - Function
  • JavaScript - if condition
  • JavaScript - switch
  • JavaScript - for loop
  • JavaScript - while loop
  • JavaScript - Scope
  • JavaScript - eval
  • JavaScript - Error Handling
  • JavaScript - strict mode
  • JavaScript - Hoisting
  • Define JS Class
  • JS Object In Depth
  • this Keyword
  • new Keyword
  • Prototype
  • Inheritance
  • Closure
  • IIFE
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge

JavaScript Date: Create, Convert, Compare Dates in JavaScript

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

Use the Date() 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 Date() 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 Date.parse() 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 Date() 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 Date() 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 -YYYY-MM-DDTHH:mm:ss.sssZ

Example: ISO Date Format
var dt = new Date('2015-02-10T10:12:50.5000z');
</code></pre>
        <div className="card-footer example-footer"></div></div>
</div>

    <h2>Convert Date Formats</h2>

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

    <p>
        The following example demonstrates <code>ToUTCString()</code>, <code>ToGMTString()</code>, <code>ToLocalDateString()</code>, and <code>ToTimeString()</code> methods to convert date into respective formats.
    </p>    <div className="card code-panel">        
           <div className="card-header example-title">Example: Date Conversion in Different Formats</div>
        <div className="panel-body"><pre className="csharpcode"><code>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 &lt;= 9 ? '0' + d : d) + '-' + (m <= 9 ? '0' + m : m) + '-' + y;
Try it
 

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

Compare Dates in JavaScript

Use comparison operators to compare two date objects.

Example: Date Comparison
var date1 = new Date('4-1-2015');
var date2 = new Date('4-2-2015');

if (date1 &gt; date2)
    alert(date1 + ' is greater than ' + date2);
else (date1 &lt; date2 )
    alert(date1 + ' is less than ' + date2);
Try it
TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

[email protected]

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.