Algorithm
-
Get the Current Date and Time:
- Use the
Date
object in JavaScript to get the current date and time.
- Use the
-
Extract Individual Components:
- Extract the year, month, day, hours, minutes, and seconds from the
Date
object.
- Extract the year, month, day, hours, minutes, and seconds from the
-
Format the Date and Time:
- Create a string to display the date and time in a readable format. You can use the extracted components to create the desired format.
-
Display the Result:
- Use a method to display the formatted date and time. This could be done by updating the content of a specific HTML element or using
console.log()
if running in a non-browser environment.
- Use a method to display the formatted date and time. This could be done by updating the content of a specific HTML element or using
Code Examples
#1 Code Example- Display Date and Time
Code -
Javascript Programming
// program to display the date and time
// get date and time
const date = new Date(2017, 2, 12, 9, 25, 30);
// get the date as a string
const n = date.toDateString();
// get the time as a string
const time = date.toLocaleTimeString();
// display date
console.log('Date: ' + n);
// display time
console.log('Time: ' + time);
Copy The Code &
Try With Live Editor
Output
Date: Sun Mar 12 2017
Time: 9:25:30 AM
Time: 9:25:30 AM
Demonstration
JavaScript Programing to Display Date and Time-DevsEnv