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.
Related
I have this code that shows the date of the next day. Is it possible to change the behavior of the dates so it changes at 17:00? For example, now the code shows the date 16.6.2020 (this is correct), and after 17:00, it should show 17.6.2020
document.querySelector("a[href='data']").setAttribute("id", "day");
document.querySelector("a[href='data2']").setAttribute("id", "day2");
var currentDate = new Date(new Date().getTime() + 24 * 60 * 60 * 1000);
var day = currentDate.getDate()
var month = currentDate.getMonth() + 1
var year = currentDate.getFullYear()
document.getElementById("day").innerHTML = day + "." + month + "." + year;
document.getElementById("day2").innerHTML = day + "." + month + "." + year;
<a class="tn-atom" href="data" id=""></a>
<a class="tn-atom" href="data2" id=""></a>
You mean this (comment currentDate.setHours(18) after test)
const pad = num => ("0"+num).slice(-2)
document.querySelector("a[href='data']").setAttribute("id", "day");
document.querySelector("a[href='data2']").setAttribute("id", "day2");
var currentDate = new Date(new Date().getTime() + 24 * 60 * 60 * 1000);
currentDate.setHours(18); // comment this when tested
if (currentDate.getHours() >=17) currentDate.setDate(currentDate.getDate()+1)
var day = currentDate.getDate()
var month = currentDate.getMonth() + 1
var year = currentDate.getFullYear()
document.getElementById("day").innerHTML = pad(day) + "." + pad(month) + "." + year;
document.getElementById("day2").innerHTML = pad(day) + "." + pad(month) + "." + year;
<a class="tn-atom" href="data" id=""></a>
<a class="tn-atom" href="data2" id=""></a>
If you don't feel like messing with time zones, add 7 hours to the date for it to show the next days date at 17:00
var currentDate = new Date(new Date().getTime() + 31 * 60 * 60 * 1000);
console.log(currentDate)
// testing DST
const aroundDST = new Date(2020,2,29,0,0,0,0); // 29th of March 2020 - DST-SST in most of Europe
const afterDST = new Date(aroundDST.getTime() + 31 * 60 * 60 * 1000);
console.log(afterDST)
I want to get next day and format it into "yyyy-MM-dd HH:mm" format, but when I run this in chrome's console, I got an Uncaught TypeError: date.getHours is not a function, why? The nextDay variable is clearly an instance of Date.
But when I removed hour and minute, just kept year, month and date, it successed, can anyone tell me the reason?
var time = new Date().getTime();
var interval = 24 * 60 * 60 * 1000;
var nextDay = new Date(time + interval);
function padding(number) {
return number < 10 ? "0" + number : "" + number;
}
function format(date) {
var year = date.getFullYear(),
month = date.getMonth(),
date = date.getDate(),
hour = date.getHours(),
minute = date.getMinutes();
return padding(year) + "-"
+ padding(month + 1) + "-"
+ padding(date) + " "
+ padding(hour) + ":"
+ padding(minute);
}
console.log(format(nextDay));
Your function takes a parameter named "date" and then tries to declare a local variable named "date". That declaration will be ignored, and the initializer will just overwrite the value of the parameter.
Change the name of the parameter:
function format(d) {
var year = d.getFullYear(),
month = d.getMonth(),
date = d.getDate(),
hour = d.getHours(),
minute = d.getMinutes();
return padding(year) + "-"
+ padding(month + 1) + "-"
+ padding(date) + " "
+ padding(hour) + ":"
+ padding(minute);
}
You are using the same variable name as the parameter, date is used twice, change the variable name like down below.
var time = new Date().getTime();
var interval = 24 * 60 * 60 * 1000;
var nextDay = new Date(time + interval);
function padding(number) {
return number < 10 ? "0" + number : "" + number;
}
function format(date) {
var year = date.getFullYear(),
month = date.getMonth(),
theDate = date.getDate(), //change the variable name
hour = date.getHours(),
minute = date.getMinutes();
return padding(year) + "-"
+ padding(month + 1) + "-"
+ padding(date) + " "
+ padding(hour) + ":"
+ padding(minute);
}
console.log(format(nextDay));
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
Timestamp:
1395660658
Code:
//timestamp conversion
exports.getCurrentTimeFromStamp = function(timestamp) {
var d = new Date(timestamp);
timeStampCon = d.getDate() + '/' + (d.getMonth()) + '/' + d.getFullYear() + " " + d.getHours() + ':' + d.getMinutes();
return timeStampCon;
};
This converts the time stamp properly in terms of time format, but the date is always:
17/0/1970
Why - cheers?
You have to multiply by 1000 as JavaScript counts in milliseconds since epoch (which is 01/01/1970), not seconds :
var d = new Date(timestamp*1000);
Reference
function convertTimestamp(timestamp) {
var d = new Date(timestamp * 1000), // Convert the passed timestamp to milliseconds
yyyy = d.getFullYear(),
mm = ('0' + (d.getMonth() + 1)).slice(-2), // Months are zero based. Add leading 0.
dd = ('0' + d.getDate()).slice(-2), // Add leading 0.
hh = d.getHours(),
h = hh,
min = ('0' + d.getMinutes()).slice(-2), // Add leading 0.
ampm = 'AM',
time;
if (hh > 12) {
h = hh - 12;
ampm = 'PM';
} else if (hh === 12) {
h = 12;
ampm = 'PM';
} else if (hh == 0) {
h = 12;
}
// ie: 2014-03-24, 3:00 PM
time = yyyy + '-' + mm + '-' + dd + ', ' + h + ':' + min + ' ' + ampm;
return time;
}
You can get the value by calling like convertTimestamp('1395660658')
Because your time is in seconds. Javascript requires it to be in milliseconds since epoch. Multiply it by 1000 and it should be what you want.
//time in seconds
var timeInSeconds = ~(new Date).getTime();
//invalid time
console.log(new Date(timeInSeconds));
//valid time
console.log(new Date(timeInSeconds*1000));
const timeStamp = 1611214867768;
const dateVal = new Date(timeStamp).toLocaleDateString('en-US');
console.log(dateVal)
I need to know the percentage remaining between two dates.
I've used this code:
$(function () {
var end = $('#data').text();
var formattedDate = new Date();
var day = formattedDate.getDate();
var month = formattedDate.getMonth();
month += 1;
var year = formattedDate.getFullYear();
if (day < 10) {
day = "0" + day;
}
if (month < 10) {
month = "0" + month;
}
var today = day + "/" + month + "/" + year;
remaining = Math.round(((end - today) * 100) / today));
alert(remaining);
});
But it does'nt work.
Any suggestion?
Thanks
You're subtracting two strings, which is why it won't work.
Subtract two Date objects instead, and you'll get the milliseconds between them (ignoring the maths as to what you define as a % of 2 dates).
var now = new Date();
var then = new Date($('#data').text());
var remaining = Math.round(((then - now) * 100) / now);
You can still, of course, get your formatted string of DD/MM/YY via;
var formattedDays = (now.getDay() < 10 ? "0" : "") + now.getDay();
var formattedMonth = (now.getMonth() < 9 ? "0" : "") + (now.getMonth() + 1);
var formattedDate = formattedDays + "/" + formattedMonth + "/" + now.getFullYear();
Note that you have an extra closing parenthesis at the end of your Math.round() line as well.