get the current date time in javascript in 12 hour format
var time = new Date();
console.log(
time.toLocaleString('en-US', { hour: 'numeric', hour12: true })
); //this will give you date object remove the outer new Date to have
//it as string
const today = new Date(new Date().toLocaleString("en-US", {
day: '2-digit',
month: '2-digit',
year: 'numeric',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
hour12: true
}));function formatAMPM(date) {
var hours = date.getHours();
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 = hours + ':' + minutes + ' ' + ampm;
return strTime;
}
console.log(formatAMPM(new Date));
Also in JavaScript:
- download a file nodejs
- forloop in js
- access to xmlhttprequest at angular 8
- how to copy to clipboard in react js
- jquery validator Date
- javascript read json file
- js match true false
- redirect react js
- jquery chrome extension
- react transition group
- scenario.getname() cucumber-js
- Line 9:6: React Hook React.useEffect has a missing dependency: 'init'. Either include it or remove the dependency array
- Javascript store in localStorage
- req.params.id in nodejs
- image react native base64
- javascript ascending and descending
- args discord.js
- angular router interview questions
- js loop backwards
- semantic ui dropdown value
- node js util promisify
- merge two objects javascript
- Replace function in string in javascript
- jquery get padding of element