I m trying to implement a method which can understand which coutry you are living and show time time exactly for this country. for example if you are living in USA showing the time with am/pm but if you are living in the Turkey showing the time for 24 hour format. I found a way to conver 24 hour system to am/pm but after that i got clueless.. Any ideas how to achieve that ?
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;
}
I think, you can use toLocaleTimeString method to show time exactly for that country:
var d = new Date();
d.toLocateTimeString('en-US'); // "12:43:04 PM"
d.toLocaleTimeString("ja-JP"); // "12:43:04"
Here's link to MDN
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleTimeString
I hope my answer will help you.
May the force be with you.
Related
I'm using Vue.
How can I convert my 24-hour time to 12-hour time when it's a string, not a date object?
For example, I need to convert "17:30" to 5:30pm.
I'm adding multiple variations of start and end times for different formatting needs: text messages, vuetify calendar, etc.
const timeAmPm=s=>s.split(':').reduce((a,c)=>a?(s===a+c)?a+c+'am':a+c+'pm':c%12+':','')
console.log(timeAmPm('17:30')) // 5:30pm
console.log(timeAmPm('10:30')) // 10:30am
Try it, it works for both - String or Date instance.
function formatAMPM(date) {
if (typeof date === "string") {
let [hours, minutes] = date.split(":");
let ampm = "AM";
if (Number(hours) > 12) {
hours = Number(hours) - 12;
ampm = "PM";
}
return `${hours}:${minutes} ${ampm}`;
} else if (date instanceof Date) {
let hours = date.getHours();
let minutes = date.getMinutes();
let ampm = hours >= 12 ? "PM" : "AM";
hours = hours % 12;
hours = hours ? hours : 12; // the hour '0' should be '12'
minutes = minutes < 10 ? "0" + minutes : minutes;
let strTime = hours + ":" + minutes + " " + ampm;
return strTime;
}
return date;
}
console.log(formatAMPM(new Date()))
console.log(formatAMPM('20:20'))
I've searched everywhere but I can't find how to do this. I'm using the Datetime-local input type using:
<input type="datetime-local" />
When a user enters the values, it is in this format on screen:
MM/DD/YYYY HH:MM AM/PM
However when the form is submitted, the datetime-local value appears in this format:
YYYY-MM-DD:THH:MM
I want to keep it in the same format as it's entered. I've searched but can't find a JavaScript that will grab the value for datetime-local and convert it to the MM/DD/YYYY HH:MM AM/PM format and set the time to either AM / PM. Current the time is in Military so anything above 12:59pm will show 13:00 for example. How can the military time also be converted?
You can use this function
function formatDate(date) {
var hours = date.getHours();
var minutes = date.getMinutes();
var ampm = hours >= 12 ? 'pm' : 'am';
hours = hours % 12;
hours = hours ? hours : 12;
minutes = minutes < 10 ? '0'+minutes : minutes;
var year = date.getFullYear();
var month = date.getMonth();
month = month < 10 ? '0'+month : month;
var date = date.getDate();
date = date < 10 ? '0' + date : date;
hours = hours < 10 ? '0' + hours : hours;
minutes = minutes < 10 ? '0' + minutes : minutes;
var strTime = month + '/' + date + '/' + year + ' ' + hours + ':' + minutes + ' ' + ampm;
return strTime;
}
console.log(formatDate(new Date));
If you want you can use moment.js in that you can format according to your need, but that library is bit heavy if you worried about your website size.
In Javascript how to check current datetime with three dates. i want to disable the radio button if date is pervious datetime.i need to validate the date according to current datetime
suppose a)10/2/2017 11:00 b)21/3/2018 11:20 c)28/4/2018 14:00
above three dates i need to block the radio button and stike the date in javascript
function formatAMPM(date) { // This is to display 12 hour format like
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;
}
var myDate = new Date();
var displayDate = myDate.getMonth()+ '/' +myDate.getDate()+ '/' +myDate.getFullYear()+ ' ' +formatAMPM(myDate);
alert(displayDate);
var dt = Date.parse(displayDate);
var d = Date.parse(ts);
var d1 = Date.parse(ts1);
var d2 = Date.parse(ts2);
alert(d);
alert(dt);
There is a good library momemt you can use that to check whether date is current date. You can use isBefore
Also if you want to use JS only you can do the following:
Convert your date string in Date object and get the time in milliseconds and then compare it to Date.now().
This question already has answers here:
How do you display JavaScript datetime in 12 hour AM/PM format?
(31 answers)
Closed 6 years ago.
I have simple code for 12hr format time
// This function gets the current time and injects it into the DOM
function updateClock() {
// Gets the current time
var now = new Date();
// Get the hours, minutes and seconds from the current time
var hours = now.getHours();
var minutes = now.getMinutes();
var seconds = now.getSeconds();
// Format hours, minutes and seconds
if (hours > 12) {
hours = hours - 12;
}
if (minutes < 10) {
minutes = "0" + minutes;
}
if (seconds < 10) {
seconds = "0" + seconds;
}
// Gets the element we want to inject the clock into
var elem = document.getElementById('clock');
// Sets the elements inner HTML value to our clock data
elem.innerHTML = hours + ':' + minutes + ':' + seconds ;
}
I want to add AM/PM please help me thanks in advance
Im just beginner on javascript
After editting your own code:
// Get the hours, minutes and seconds from the current time
var hours = now.getHours();
var minutes = now.getMinutes();
var seconds = now.getSeconds();
var amOrPm = 'AM';
// Format hours, minutes and seconds
if (hours > 12) {
amOrPm = 'PM';
hours = hours - 12;
}
if (minutes < 10) {
minutes = "0" + minutes;
}
if (seconds < 10) {
seconds = "0" + seconds;
}
// Gets the element we want to inject the clock into
var elem = document.getElementById('clock');
// Sets the elements inner HTML value to our clock data
elem.innerHTML = hours + ':' + minutes + ':' + seconds + ' ' + amOrPm;
Try this...
var ampm = hours >= 12 ? 'pm' : 'am';
then
elem.innerHTML = hours + ':' + minutes + ':' + seconds + ' ' + ampm;
I don't know if you want to code it by yourself, but there is a really really great library which handles everything with date stuff you can imagine.
Just checkout momentjs.
It's an easy AF lib to transform dates (also in pm/am).
If you've any question :) just comment ...
I have the follow function that properly returns the date in the format required, but the value returned isn't respecting the local timezone. In this case, its 4 hours off. What would I need to modify in this function to make it add the proper offsets based on the users location?
Thanks!
function date_str(seconds) {
var dt = new Date(1970, 0, 1);
dt.setSeconds(seconds);
console.log(dt);
var month = dt.getMonth() + 1;
var date = dt.getDate();
var year = dt.getFullYear();
var hours = dt.getHours();
var minutes = dt.getMinutes();
var ampm = hours >= 12 ? 'PM' : 'AM';
hours = hours % 12;
hours = hours ? hours : 12;
minutes = minutes < 10 ? '0' + minutes : minutes;
return month + '/' + date + '/' + year + ', ' + hours + ':' + minutes + ' ' + ampm;
}
Edit: Passing unix time to function 1396450616.505 which converts to Wed, 02 Apr 2014 14:56:56 GMT which returns Sent at 4/2/2014, 2:56 PM from the function itself. The time here is 10:56 AM EST.
Assuming that seconds is a unix epoch (UTC), you should just use
function date_str(seconds) {
var dt = new Date(seconds*1000);
console.log(dt);
…
instead. The get…() methods will respect the local timezone. If you don't want that, you should use the getUTC…() equivalents.