i want to display AM / PM using javascript [duplicate] - javascript

This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Closed 3 years ago.
i try to disply time with the showing AM / PM format but i am unable to find any code can you please guide me
var dt = new Date();
var time = dt.getHours() + ":" + dt.getMinutes() + ":" + dt.getSeconds();
document.getElementById("dt").innerHTML = time;
<p id='dt'></p>

var dt = new Date();
var time = dt.getHours() + ":" + dt.getMinutes() + ":" + dt.getSeconds()+" ";
time+= dt.getHours()>=12?"PM":"AM"
document.getElementById("dt").innerHTML = time;
<div id="dt"></div>

Just check if the value less than 12, and keep the hours under 12 and return 12 instead of 0 by: (hours %12 || 12):
var dt = new Date();
var time = (dt.getHours()%12||12) + ":" + dt.getMinutes() + ":" + dt.getSeconds() + " " + (dt.getHours() < 12)===0?"AM" : "PM";
document.getElementById("dt").innerHTML = time;

Just compare the hours to if its less than 12 and if so set a variable to either AM or PM. Note that the following has the leading 0 added to the mins and secs if required (the slice will only include the 0 if the length of the value is 1).
var dt = new Date();
var hrs = dt.getHours();
var hours = hrs % 12;
var mins = '0' + dt.getMinutes();
var minutes = mins.slice(-2);
var secs = '0' + dt.getSeconds();
var seconds = secs.slice(-2);
var amPm = hrs< 12 ? 'AM' : 'PM';
var time = hours + ":" + minutes + ":" + seconds + ' ' + amPm;
document.getElementById("dt").innerHTML = time;
<p id = "dt"></p>

Related

I nee to add "0" before minutes <10 in Typescript

I need to display the time and the minutes are not working correctly. It is returning 12:4, 2:3..when the minutes are less than 10 there is not 0. I tried adding the "0" like this:
var formattedTime = new Date(time + 'z');
var hours = formattedTime.getHours();
var amOrPm = hours >= 12 ? 'PM' : 'AM';
hours = (hours % 12) || 12;
var minutes = formattedTime.getMinutes();
if (minutes < 10) {
("0" + minutes)
};
var finalTime = eventDate +" "+ hours + ":" + minutes + " " + amOrPm;
Any help is welcome(I am new to coding). Thank You.
The statement ("0" + minutes) doesn't do anything. It does add a '0' to minutes, but you're not doing anything with the result. The problem is that you need to set the result of this statement in a new variable.
But here's an easier way to do this:
const minutes = 5;
const minuteStr = minutes.toString().padStart(2, '0');

I want to subtract 8 hours from current time. If I try to subtract 8 from hours, it goes in negative

I am using UTC time and want to subtract 8 hours from the current time. I am using JavaScript.
function getUTCStartDate(time) {
var d = new Date();
var year = d.getUTCFullYear();
var month = (d.getUTCMonth() + 1);
var date = d.getUTCDate();
var hours = d.getUTCHours();
var hoursMinusTime = (d.getUTCHours() - time);
var minutes = d.getUTCMinutes();
var seconds = d.getUTCSeconds();
var millisec = d.getUTCMilliseconds();
date = date.toString().length > 1 ? date : '0' + date;
month = month.toString().length > 1 ? month : '0' + month;
hoursMinusTime = hoursMinusTime.toString().length > 1 ? hoursMinusTime : '0' + hoursMinusTime;
minutes = minutes.toString().length > 1 ? minutes : '0' + minutes;
seconds = seconds.toString().length > 1 ? seconds : '0' + seconds;
millisec = millisec.toString().length > 1 ? millisec : '0' + millisec;
return year + "-" + month + "-" + date + "T" + hoursMinusTime + ":" + minutes + ":" + seconds + "." + millisec + "Z";
}
I did it in a different way. Converted the time in unix time deducted 8 hours from it.
var currTime = new Date();
var d = new Date(currTime.getTime() - (time * 3600000));
var hours = d.getUTCHours();

How to show current date with time AM PM and CST using jquery

Im trying to show current Date with below format
7/28/2016 11:55:37 PM CST
Date is object of Javascript, Jquery also uses object of javaScript. I hope below code will be usefull for your.
function formatDate(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 date.getMonth()+1 + "/" + date.getDate() + "/" + date.getFullYear() + " " + strTime;
}
var d = new Date();
var e = formatDate(d);
alert(e);

Javascript Add minutes to date not working in IE and Safari

I want to add minutes to a date and display it.I am using the following code
function dt(){
var d = new Date();
d.setMinutes(d.getMinutes()+15*60);
var theDate = d.getFullYear() + '-' + ( d.getMonth() + 1 ) + '-' + d.getDate()+' '+d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();
//var d1 = new Date( Date.parse( theDate ) + s1*60*1000 );
var d1=new Date(theDate);
var hours = d1.getHours();
var minutes = d1.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 + ' ';
var t=( d1.getMonth() + 1 )+ '/' + d1.getDate() + '/' + d1.getFullYear() +' '+strTime; alert(t+ampm);
}
dt();
This code is working fine in Chrome.But in IE and safari, it was returning NAN:NAN:NAN 12:NAN AM.
Anyone please help.
Regards
Rekha
The line:
d.setMinutes(d.getMinutes()+15*60);
will add 15 hours to the date. Why not:
d.setHours(d.getHours()+15);
Then you copy a date by creating a string then parsing it:
var theDate = d.getFullYear() + '-' + ... + d.getSeconds();
var d1 = new Date(theDate);
Do not do that. Ever. Parsing date strings is unreliable (as you've discovered) and not recommended. To copy a date, use:
var d1 = new Date(+d);
Try replacing:
var d1 = new Date(theDate);
with:
var d1 = new Date(d.getFullYear(), d.getMonth() + 1, d.getDate(), d.getHours(), d.getMinutes(), d.getSeconds());
Using the Date contructor with a date string has some limitations

Get HTML5 datetime-local exact value

Try to use this, http://jsfiddle.net/mdg2u4ut and you will notice the hour will be different with what you've set, like in my case
I think it's because of the timezone problem.
I can just hardcoded -8 for the hour variable in my case but that's not the smart way of doing it.
<input type="datetime-local" onblur="formatDate(this.value)" />
<p id="para"></p>
my JS
function formatDate(date) {
if(date){
date = new Date(date);
var hours = date.getHours();
var minutes = date.getMinutes();
var format = hours < 12 ? 'AM' : 'PM';
hours = hours % 12;
hours = hours ? hours : 12; // making 0 a 12
minutes = minutes < 10 ? '0'+minutes : minutes;
var time = hours + ':' + minutes + ' ' + format;
var output = date.getMonth()+1 + "/" + date.getDate() + "/" + date.getFullYear() + " " + time;
document.querySelector('#para').innerHTML = output;
}
}
Use getUTC methods instead. jsFiddle
var hours = date.getUTCHours();
var minutes = date.getUTCMinutes();
var format = hours < 12 ? 'AM' : 'PM';
hours = hours % 12;
hours = hours ? hours : 12; // making 0 a 12
minutes = minutes < 10 ? '0'+minutes : minutes;
var time = hours + ':' + minutes + ' ' + format;
var output = date.getUTCMonth()+1 + "/" + date.getUTCDate() + "/" + date.getUTCFullYear() + " " + time;

Categories

Resources