Algorithm


  1. Get the Current Date and Time:

    • Use the Date object in JavaScript to get the current date and time.
  2. Extract Individual Components:

    • Extract the year, month, day, hours, minutes, and seconds from the Date object.
  3. 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.
  4. 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.

 

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

x
+
cmd
Date: Sun Mar 12 2017
Time: 9:25:30 AM
Advertisements

Demonstration


JavaScript Programing to Display Date and Time-DevsEnv

Previous
JavaScript Practice Example #3 - Assign 3 Variables and Print Good Way