🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Introduction
In many applications, obtaining the current date and time is a common task. JavaScript provides the Date object to easily retrieve the current date and time in various formats. This program demonstrates how to get and display the current date and time using JavaScript.
Problem Statement
Create a JavaScript program that:
- Retrieves the current date and time.
- Displays the result in a readable format.
Example:
- Input: No input required.
- Output: Current date and time in a human-readable format, such as
"September 6, 2024, 10:30:00 AM"
Solution Steps
- Get the Current Date and Time: Use the
Date()object to get the current date and time. - Format the Date and Time: Format the date and time into a readable string.
- Display the Result: Print the formatted date and time.
JavaScript Program
// JavaScript Program to Get the Current Date and Time
// Author: https://www.javaguides.net/
function getCurrentDateTime() {
// Step 1: Create a new Date object to get the current date and time
let currentDateTime = new Date();
// Step 2: Format the date and time
let date = currentDateTime.toLocaleDateString();
let time = currentDateTime.toLocaleTimeString();
// Step 3: Return the formatted result
return `${date}, ${time}`;
}
// Display the current date and time
let result = getCurrentDateTime();
console.log(`The current date and time is: ${result}`);
Output
The current date and time is: 9/6/2024, 10:30:00 AM
Example with Different Time Zones
let currentDateTime = new Date();
let timeInNewYork = currentDateTime.toLocaleString("en-US", { timeZone: "America/New_York" });
console.log(`The current time in New York is: ${timeInNewYork}`);
Output:
The current time in New York is: 9/6/2024, 10:30:00 AM
Explanation
Step 1: Create a New Date Object
- The
Date()object is used to retrieve the current date and time. When instantiated, it holds the exact date and time at the moment the object is created.
Step 2: Format the Date and Time
- The
toLocaleDateString()method returns the date in a human-readable format (e.g.,MM/DD/YYYYorDD/MM/YYYY, depending on locale). - The
toLocaleTimeString()method returns the time in a human-readable format (e.g.,10:30:00 AM).
Step 3: Return and Display the Result
- The formatted date and time are combined into a string and printed using
console.log().
Conclusion
This JavaScript program demonstrates how to retrieve and display the current date and time using the Date() object. By using the toLocaleDateString() and toLocaleTimeString() methods, you can format the date and time in a readable way, making this solution ideal for displaying timestamps or logging events in applications.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment