The task is to print the system’s current day and time in the following format(Time in hours, minutes and milliseconds).
Use getDay() function to create an array of days in a week. It returns a number of day like 0 for Sunday, 1 for Monday and so on). Convert it into AM or PM using ternary operator.
Use getHours() method to get the hour value between 0 to 23. It returns an integer value.
Minutes and milliseconds were printed using getMinutes() and getMilliseconds() functions respectively.
print current day and time
var myDate = new Date();
var myDay = myDate.getDay();
// Array of days.
var weekday = ['Sunday', 'Monday', 'Tuesday',
'Wednesday', 'Thursday', 'Friday', 'Saturday'
document.write("Today is : " + weekday[myDay]);
// get hour value.
var hours = myDate.getHours();
var ampm = hours >= 12 ? 'PM' : 'AM';
hours = hours % 12;
hours = hours ? hours : 12;
var minutes = myDate.getMinutes();
minutes = minutes <
? '0' + minutes : minutes;
+ " " + ampm + " : " + minutes +
" : " + myDate.getMilliseconds();
document.write("\tCurrent time is : " + myTime);
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses
are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!