Get Current Date and Time in JavaScript

Get current date and time in various formats

BeginnerDate/Time ProgramsExample 1 of 10
get-current-date-and-time.js
1// Method 1: Current date and time
2const now = new Date();
3console.log('Current date:', now);
4
5// Method 2: Get individual components
6const date = new Date();
7console.log('Year:', date.getFullYear());
8console.log('Month:', date.getMonth() + 1); // 0-11, so add 1
9console.log('Date:', date.getDate());
10console.log('Day:', date.getDay()); // 0-6 (Sunday-Saturday)
11console.log('Hours:', date.getHours());
12console.log('Minutes:', date.getMinutes());
13console.log('Seconds:', date.getSeconds());
14console.log('Milliseconds:', date.getMilliseconds());
15
16// Method 3: ISO string
17const isoString = new Date().toISOString();
18console.log('ISO String:', isoString);
19
20// Method 4: Locale string
21const localeString = new Date().toLocaleString();
22console.log('Locale String:', localeString);
23
24const localeDateString = new Date().toLocaleDateString();
25console.log('Locale Date:', localeDateString);
26
27const localeTimeString = new Date().toLocaleTimeString();
28console.log('Locale Time:', localeTimeString);
29
30// Method 5: UTC methods
31const utcDate = new Date();
32console.log('UTC Year:', utcDate.getUTCFullYear());
33console.log('UTC Month:', utcDate.getUTCMonth() + 1);
34console.log('UTC Date:', utcDate.getUTCDate());
35console.log('UTC Hours:', utcDate.getUTCHours());
36
37// Method 6: Timestamp
38const timestamp = Date.now();
39console.log('Timestamp:', timestamp);
40
41const timestampFromDate = new Date().getTime();
42console.log('Timestamp from Date:', timestampFromDate);
43
44// Method 7: Date string
45const dateString = new Date().toString();
46console.log('Date String:', dateString);
47
48// Method 8: Custom format
49function formatDate(date) {
50 const year = date.getFullYear();
51 const month = String(date.getMonth() + 1).padStart(2, '0');
52 const day = String(date.getDate()).padStart(2, '0');
53 const hours = String(date.getHours()).padStart(2, '0');
54 const minutes = String(date.getMinutes()).padStart(2, '0');
55 const seconds = String(date.getSeconds()).padStart(2, '0');
56
57 return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
58}
59
60console.log('Formatted:', formatDate(new Date()));
61
62// Method 9: Timezone offset
63const offset = new Date().getTimezoneOffset();
64console.log('Timezone Offset (minutes):', offset);
65
66// Method 10: Date.now() vs new Date()
67console.log('Date.now():', Date.now()); // Number
68console.log('new Date():', new Date()); // Date object

Output

Current date: 2024-01-15T10:30:45.123Z
Year: 2024
Month: 1
Date: 15
Day: 1
Hours: 10
Minutes: 30
Seconds: 45
Milliseconds: 123
ISO String: 2024-01-15T10:30:45.123Z
Locale String: 1/15/2024, 10:30:45 AM
Locale Date: 1/15/2024
Locale Time: 10:30:45 AM
UTC Year: 2024
UTC Month: 1
UTC Date: 15
UTC Hours: 10
Timestamp: 1705315845123
Timestamp from Date: 1705315845123
Date String: Mon Jan 15 2024 10:30:45 GMT+0000 (UTC)
Formatted: 2024-01-15 10:30:45
Timezone Offset (minutes): 0
Date.now(): 1705315845123
new Date(): 2024-01-15T10:30:45.123Z

What's going on

Date objects represent dates and times.

Date Creation

new Date(): Current date/time
new Date(timestamp): From timestamp
new Date(year, month, day): Specific date
Date.now(): Current timestamp

Date Methods

getFullYear/getMonth/getDate
getHours/getMinutes/getSeconds
getTime: Timestamp
toISOString: ISO format

Locale Methods

toLocaleString: Local format
toLocaleDateString: Date only
toLocaleTimeString: Time only

UTC Methods

getUTCFullYear/getUTCMonth
getUTCHours/getUTCMinutes
Work with UTC timezone

Important Notes

Month is 0-indexed (0-11)
Day of week: 0=Sunday, 6=Saturday
getTime() returns milliseconds