Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript to display the current date and time

Tags:

javascript

I have the following test Script to display the current date & time :-

document.getElementById("para1").innerHTML = formatAMPM();  function formatAMPM() {     var date = new Date();     var hours = date.getHours();     var days = date.getDay();      var minutes = date.getMinutes();     var ampm = hours >= 12 ? 'pm' : 'am';     hours = hours % 12;     hours = hours ? hours : 12; // the hour '0' should be '12'     minutes = minutes < 10 ? '0'+minutes : minutes;     var strTime = date + ' ' + hours + ':' + minutes + ' ' + ampm;     return strTime; } 

which will display the following :-

Fri Aug 30 2013 16:36:10 GMT+0100 (GMT Standard Time) 4:36 pm 

but i need to modify this to display only:-

Fri Aug 30 2013 4:36 pm 

can anyone advice on how i can achieve this ?

like image 344
john Gu Avatar asked Aug 30 '13 15:08

john Gu


People also ask

How do you get current date and time in JavaScript?

In JavaScript, we can easily get the current date or time by using the new Date() object. By default, it uses our browser's time zone and displays the date as a full text string, such as "Fri Jun 17 2022 10:54:59 GMT+0100 (British Summer Time)" that contains the current date, time, and time zone.

How do I display the current date and time in HTML?

HTML <time> Tag.

Can JavaScript handle dates and time?

The date and time is broken up and printed in a way that we can understand as humans. JavaScript, however, understands the date based on a timestamp derived from Unix time, which is a value consisting of the number of milliseconds that have passed since midnight on January 1st, 1970.


2 Answers

Demo using Console.Log

// get a new date (locale machine date time)  var date = new Date();  // get the date as a string  var n = date.toDateString();  // get the time as a string  var time = date.toLocaleTimeString();    // log the date in the browser console  console.log('date:', n);  // log the time in the browser console  console.log('time:',time);

Demo using a DIV

// get a new date (locale machine date time)  var date = new Date();  // get the date as a string  var n = date.toDateString();  // get the time as a string  var time = date.toLocaleTimeString();    // find the html element with the id of time  // set the innerHTML of that element to the date a space the time  document.getElementById('time').innerHTML = n + ' ' + time;
<div id='time'></div>

Note: these functions aren't fully cross browser supported

Cross-Browser Functional

//Fri Aug 30 2013 4:36 pm  console.log(formatAMPM(new Date()));    //using your function (passing in date)  function formatAMPM(date) {      // gets the hours      var hours = date.getHours();      // gets the day      var days = date.getDay();      // gets the month      var minutes = date.getMinutes();      // gets AM/PM      var ampm = hours >= 12 ? 'pm' : 'am';      // converts hours to 12 hour instead of 24 hour      hours = hours % 12;      // converts 0 (midnight) to 12      hours = hours ? hours : 12; // the hour '0' should be '12'      // converts minutes to have leading 0      minutes = minutes < 10 ? '0'+ minutes : minutes;          // the time string      var time = hours + ':' + minutes + ' ' + ampm;          // gets the match for the date string we want      var match = date.toString().match(/\w{3} \w{3} \d{1,2} \d{4}/);          //the result      return match[0] + ' ' + time;  }
like image 139
abc123 Avatar answered Sep 26 '22 02:09

abc123


Try this:

var d = new Date(),     minutes = d.getMinutes().toString().length == 1 ? '0'+d.getMinutes() : d.getMinutes(),     hours = d.getHours().toString().length == 1 ? '0'+d.getHours() : d.getHours(),     ampm = d.getHours() >= 12 ? 'pm' : 'am',     months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'],     days = ['Sun','Mon','Tue','Wed','Thu','Fri','Sat']; return days[d.getDay()]+' '+months[d.getMonth()]+' '+d.getDate()+' '+d.getFullYear()+' '+hours+':'+minutes+ampm; 

DEMO

like image 36
Flame Trap Avatar answered Sep 22 '22 02:09

Flame Trap