Javascript Add minutes to date not working in IE and Safari - javascript

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

Related

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

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>

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 Date Object - Different Value in JSFiddle

This is a simple one I think.
Why do I get 2015-11-04 when I run the following code in JSFiddle (new Date(1451606399999), but when I run the same code in my browser console I get 2015-12-31 (which is the value I'm expecting).
I would have thought any in either case the timezone would be the same as the code is running on the client, and why would timezone make more than a month difference in the date?
function test()
{
var date = new Date(1451606399999);
var year = date.getFullYear();
var month = date.getMonth();
var day = date.getDay();
var hours = date.getHours();
var minutes = "0" + date.getMinutes();
var seconds = "0" + date.getSeconds();
var formattedTime = year + '-' + month + '-' + day + ' ' + hours + ':' + minutes.substr(-2) + ':' + seconds.substr(-2);
document.getElementById("myDiv").innerHTML = formattedTime;
}
JSFiddle with code
Confusingly, date.getMonth() is 0 based, meaning 0 is January, so it should be month = date.getMonth()+1. Also confusingly date.getDay() actually returns the day of the week (0 is Sunday, 1 is Monday... etc). The function you're looking for is date.getDate()
function test(){
var date = new Date(1451606399999);
var year = date.getFullYear();
var month = date.getMonth()+1;
var day = date.getDate();
var hours = date.getHours();
var minutes = "0" + date.getMinutes();
var seconds = "0" + date.getSeconds();
var formattedTime = year + '-' + month + '-' + day + ' ' + hours + ':' + minutes.substr(-2) + ':' + seconds.substr(-2);
console.log(date);
document.getElementById("myDiv").innerHTML = formattedTime;
}

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;

javascript date + 7 days

What's wrong with this script?
When I set my clock to say 29/04/2011 it adds 36/4/2011 in the week input! but the correct date should be 6/5/2011
var d = new Date();
var curr_date = d.getDate();
var tomo_date = d.getDate()+1;
var seven_date = d.getDate()+7;
var curr_month = d.getMonth();
curr_month++;
var curr_year = d.getFullYear();
var tomorrowsDate =(tomo_date + "/" + curr_month + "/" + curr_year);
var weekDate =(seven_date + "/" + curr_month + "/" + curr_year);
{
jQuery("input[id*='tomorrow']").val(tomorrowsDate);
jQuery("input[id*='week']").val(weekDate);
}
var date = new Date();
date.setDate(date.getDate() + 7);
console.log(date);
And yes, this also works if date.getDate() + 7 is greater than the last day of the month. See MDN for more information.
Without declaration
To return timestamp
new Date().setDate(new Date().getDate() + 7)
To return date
new Date(new Date().setDate(new Date().getDate() + 7))
Something like this?
var days = 7;
var date = new Date();
var res = date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
alert(res);
convert to date again:
date = new Date(res);
alert(date)
or alternatively:
date = new Date(res);
// hours part from the timestamp
var hours = date.getHours();
// minutes part from the timestamp
var minutes = date.getMinutes();
// seconds part from the timestamp
var seconds = date.getSeconds();
// will display time in 10:30:23 format
var formattedTime = date + '-' + hours + ':' + minutes + ':' + seconds;
alert(formattedTime)
In One line:
new Date(Date.now() + 7 * 24 * 60 * 60 * 1000)
The simple way to get a date x days in the future is to increment the date:
function addDays(dateObj, numDays) {
return dateObj.setDate(dateObj.getDate() + numDays);
}
Note that this modifies the supplied date object, e.g.
function addDays(dateObj, numDays) {
dateObj.setDate(dateObj.getDate() + numDays);
return dateObj;
}
var now = new Date();
var tomorrow = addDays(new Date(), 1);
var nextWeek = addDays(new Date(), 7);
alert(
'Today: ' + now +
'\nTomorrow: ' + tomorrow +
'\nNext week: ' + nextWeek
);
Using the Date object's methods will could come in handy.
e.g.:
myDate = new Date();
plusSeven = new Date(myDate.setDate(myDate.getDate() + 7));
var days = 7;
var date = new Date();
var res = date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
var d = new Date(res);
var month = d.getMonth() + 1;
var day = d.getDate();
var output = d.getFullYear() + '/' +
(month < 10 ? '0' : '') + month + '/' +
(day < 10 ? '0' : '') + day;
$('#txtEndDate').val(output);
var future = new Date(); // get today date
future.setDate(future.getDate() + 7); // add 7 days
var finalDate = future.getFullYear() +'-'+ ((future.getMonth() + 1) < 10 ? '0' : '') + (future.getMonth() + 1) +'-'+ future.getDate();
console.log(finalDate);
You can add or increase the day of week for the following example and hope this will helpful for you.Lets see....
//Current date
var currentDate = new Date();
//to set Bangladeshi date need to add hour 6
currentDate.setUTCHours(6);
//here 2 is day increament for the date and you can use -2 for decreament day
currentDate.setDate(currentDate.getDate() +parseInt(2));
//formatting date by mm/dd/yyyy
var dateInmmddyyyy = currentDate.getMonth() + 1 + '/' + currentDate.getDate() + '/' + currentDate.getFullYear();
Two problems here:
seven_date is a number, not a date. 29 + 7 = 36
getMonth returns a zero based index of the month. So adding one just gets you the current month number.

Categories

Resources