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 ?
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.
HTML <time> Tag.
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.
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);
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
//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; }
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With