Date
JavaScript Date Tutorial
Table of Contents
Introduction to JavaScript Date
Creating Dates in JavaScript
Using
new Date()
Creating Date with String Format
Creating Date with Date and Time Components
Creating Date with Timestamps
Date Methods
Getting Date Components
getFullYear()
getMonth()
getDate()
getDay()
Getting Time Components
getHours()
getMinutes()
getSeconds()
getMilliseconds()
Setting Date and Time Components
setFullYear()
setMonth()
setDate()
setHours()
setMinutes()
setSeconds()
setMilliseconds()
Formatting Dates
Using
toDateString()
Using
toLocaleDateString()
Using
toISOString()
Using
toLocaleTimeString()
Working with Time Zones
Getting UTC Date and Time
Getting Timezone Offset
Comparing Dates
Using Relational Operators
Using
getTime()
Method
Date Calculations
Adding/Subtracting Days
Finding Difference Between Dates
Conclusion
1. Introduction to JavaScript Date
Welcome to the Codes with Pankaj tutorial on JavaScript Date! In this tutorial, we will explore how to work with dates and times in JavaScript. By the end of this tutorial, you’ll be able to create, manipulate, and format dates with ease. Let’s get started!
JavaScript provides a built-in Date
object that allows you to work with dates and times. The Date
object is powerful and versatile, letting you perform various operations like getting the current date, formatting dates, and performing date calculations.
2. Creating Dates in JavaScript
There are several ways to create dates in JavaScript.
Using new Date()
The simplest way to create a new date is by using the new Date()
constructor without any arguments. This will create a date object with the current date and time.
Example:
Creating Date with String Format
You can create a date by passing a date string to the Date
constructor.
Example:
Creating Date with Date and Time Components
You can specify the year, month, day, hours, minutes, seconds, and milliseconds when creating a date.
Example:
Creating Date with Timestamps
You can create a date by passing the number of milliseconds since January 1, 1970 (known as the Unix Epoch).
Example:
3. Date Methods
Getting Date Components
getFullYear()
This method returns the year of the specified date.
getMonth()
This method returns the month of the specified date (0-11).
getDate()
This method returns the day of the month (1-31).
getDay()
This method returns the day of the week (0-6, where 0 is Sunday).
Getting Time Components
getHours()
This method returns the hour of the specified date (0-23).
getMinutes()
This method returns the minutes (0-59).
getSeconds()
This method returns the seconds (0-59).
getMilliseconds()
This method returns the milliseconds (0-999).
Setting Date and Time Components
You can also modify the date and time using setter methods.
setFullYear()
Sets the year.
setMonth()
Sets the month.
setDate()
Sets the day of the month.
setHours()
Sets the hour.
setMinutes()
Sets the minutes.
setSeconds()
Sets the seconds.
setMilliseconds()
Sets the milliseconds.
4. Formatting Dates
JavaScript provides several methods to format dates as strings.
Using toDateString()
Returns the date in a human-readable format.
Using toLocaleDateString()
Returns the date in a format based on the local region.
Using toISOString()
Returns the date in ISO format (YYYY-MM-DDTHH:mm:ss.sssZ).
Using toLocaleTimeString()
Returns the time in a format based on the local region.
5. Working with Time Zones
JavaScript Date objects work with the local time zone by default, but you can also work with UTC (Coordinated Universal Time).
Getting UTC Date and Time
You can retrieve the UTC components using methods like getUTCFullYear()
, getUTCMonth()
, etc.
Example:
Getting Timezone Offset
You can get the difference between UTC time and local time in minutes using getTimezoneOffset()
.
Example:
6. Comparing Dates
Using Relational Operators
You can compare two dates using relational operators like <
, >
, <=
, >=
.
Example:
Using getTime()
Method
You can compare dates by converting them to timestamps using getTime()
.
Example:
7. Date Calculations
Adding/Subtracting Days
To add or subtract days from a date, you can manipulate the day component.
Example:
Finding Difference Between Dates
You can find the difference between two dates by subtracting their timestamps.
Example:
8. Conclusion
In this detailed tutorial, we've covered the basics of working with dates and times in JavaScript. From creating dates to formatting and performing date calculations, you now have the tools to manage dates in your JavaScript projects.
For more tutorials and examples, visit www.codeswithpankaj.com! Happy coding!
Last updated