The following code:
//var d = new Date(year, month, day, hours, minutes, seconds, milliseconds);
var today = new Date(2013,0,31);
var tomorrow = new Date();
tomorrow.setDate(today.getDate() + 1);
alert("New date is "+tomorrow.getFullYear() +", "+ tomorrow.getMonth()+", "+ tomorrow.getDate())
...outputs: 2014, 1, 1
(Demo: http://jsfiddle.net/3pA3Q/5/)
Can anyone explain this?
Also, these two have the same result:
var today = new Date(2013,11,31);
var today = new Date(2013,12,31);
I understand "month beginning with 0 for January to 11 for December", so new Date(2013,12,31) should be Year 2014, January, 31st
You initialised tomorrow to be todays date, so in this line tomorrow.setDate(today.getDate() + 1); you're simply adding 1 day to todays date.
You would be better off cloning your date:
var today = new Date(2013,0,31);
var tomorrow = new Date(today.getTime()); // Get a copy
tomorrow.setDate(tomorrow.getDate() + 1);
Related
How can I get the day that the next month starts on? (Monday Tuesday etc.).
I tried the following but it returns more than just the day.
var date = new Date(), y = date.getFullYear(), m = date.getMonth();
var firstDay = new Date(y, m, 1);
var lastDay = new Date(y, m + 1, 0);
You're almost there, just add this after your last line of code to get the name of the day
var dayName = lastDay.toLocaleDateString('en-us', { weekday: 'long' });
Also keep in mind that Date constructor's second parameter is monthIndex MDN.
You should add m by 1 to get next month's first day and add it by 2 and provide date = 0 to get the last day of the next month.
var date = new Date(), y = date.getFullYear(), m = date.getMonth();
var firstDay = new Date(y, m + 1, 1);
var lastDay = new Date(y, m + 2, 0);
var firstDayName = firstDay .toLocaleDateString('en-us', { weekday: 'long' });
var lastDayName = lastDay.toLocaleDateString('en-us', { weekday: 'long' });
console.log(firstDayName, lastDayName)
Your current code is getting Date instances for the first and last day of the current month. So first you'll need to fix that. Then, if you want day numbers instead, you have to call getDay to get the values (0 = Sunday through 6 = Saturday):
const today = new Date();
const year = today.getFullYear();
const month = today.getMonth() + 1; // + 1 = next month
const firstDay = new Date(year, month, 1).getDay();
const lastDay = new Date(year, month + 1, 0).getDay();
console.log({firstDay, lastDay});
As I write this on July 31st, that shows 1 and 3, telling us that August 2022 starts on Monday and ends on Wednesday.
I just took the current date and got the first day of the month. Then we just converted this to string. It's working. Try this:
const date = new Date(); // current date 1/8/2022
const nextMonth = new Date(date.getFullYear(), date.getMonth() + 1, 1); //first day of next month
const nextMonthStartDay = nextMonth.toLocaleDateString('en-US', { weekday: 'long' }); //convert to date string
console.log(nextMonthStartDay) //Thursday
I would like to know how to get the first and last date of the current year. Example in this format: 2020-01-01 and 2020-12-31. Thanks!
I have this initial code that gets the current date today. This might help.
var today = new Date();
var date = today.getFullYear() + '-' + ('0' + (today.getMonth()+1)).slice(-2) + '-' + ('0' + today.getDate()).slice(-2);
You can get the first date of the current year as follows.
var d = new Date(new Date().getFullYear(), 0, 1);
You can get the last date of the current year as follows.
var d = new Date(new Date().getFullYear(), 11, 31);
This is how you can calculate the current date:
var currentDate = new Date();
You can instantiate Date using year, month and day, but keep in mind that month is indexed from 0:
var theFirst = new Date(currentDate.getFullYear(), 0, 1);
var theLast = new Date(currentDate.getFullYear(), 11, 31);
This seems a lot like homework, so I'm not going to give you a code block to copy and paste.
You seem to already know how to get the current year, and the year always goes from 01-01 to 12-31. So do what you already have, but instead of today's date, use 01-01 and 12-31
In c# it's enough to do:
new DateTime(year, month, day);
How should I do in JS?
Is the following code correct?
var birthYear = parseInt($("#BirthYear").valueOf());
var birthMonth = parseInt($("#BirthMonth").valueOf());
var birthDay = parseInt($("#BirthYear").valueOf());
var birthDate = new Date(birthYear, birthMonth + 1, birthDay);
var whateverName = new Date(year, month, day);
Month is 0 indexed, so you need to put in 0 if you want January etc.
You can also enter negative months and dates. This will "count backwards" that many months or days.
This allows you to get the last date of a month by entering the month ahead of it and then 0 as the day.
var dateObject = new Date();
dateObject.getFullYear() + " " + dateObject.getMonth() + 1 + " " + dateObject.getDate();
JavaScript Dates
new Date()
new Date(milliseconds)
new Date(dateString)
new Date(year, month, day, hours, minutes, seconds, milliseconds)
i have timeline script, and i want, that this script would work as current month.
var timeline = new Timeline("timeline", new Date("Mar 2013"));
I need to replace "Mar 2013" with "Mar 2014", but i dont want to write it with hand. I mean, that in future, script should automaticaly get current year and month. Is this even possible?
Thanks to all of you for any answers!
"Is this even possible?"
Of course.
Using new Date() without arguments gets you a date object with the (full) current date, so:
var timeline = new Timeline("timeline", new Date());
Or if you want midnight on the first day of the current month:
var now = new Date();
var timeline = new Timeline("timeline", new Date(now.getFullYear(), now.getMonth()));
var months = ["January","February","March","April","May","June",
"July","August","September","October","November","December"];
var date = new Date();
var year = date.getFullYear();
var month = months[date.getMonth()];
var returnDate = month + ' ' + year;
var timeline = new Timeline("timeline", returnDate);
How do I take today's date and add 1 day to it?
If possible, inline please?
This will get tomorrow's date:
var a = new Date((new Date()).valueOf() + 1000*3600*24);
You have to use the getDate() and setDate() methods of the Date object which respectively get and set the day value of the date.
var date = new Date();
date.setDate(date.getDate() + 1);
Check the MDC Date object reference for more information on working with dates
Try this:
//create the date
var myDate = new Date();
//add a day to the date
myDate.setDate(myDate.getDate() + 1);
dt = new Date();
dt.setDate(dt.getDate() + 1);
If by "add 1 day to it" you mean "add 24 hours", that is, add 24*60*60*1000 milliseconds to a JavaScript date object, then the correct solution is:
var d = new Date();
d.setTime(d.getTime() + 86400000);
console.log('24 hours later');
console.log(d);
As #venkatagiri pointed out in an earlier comment, this will in fact add 24 hours to the current JavaScript date object in all scenarios, while d.setDate(d.getDate() + 1) will NOT if a Daylight Savings Time cross-over is involved. See this JSFiddle to see the difference in context of the 2013 start of DST (at March 10, 2013 at 2:00 AM, DST locale time moved forward an hour). setDate() in this scenario only adds 23 hours, while setTime() adds 24.
var d = new Date();
var curr_date = d.getDate();
var n =curr_date;
jQuery(".class_name:eq(0)").text(n);
var m =[d.getDate()+1];
jQuery(".class_name:eq(1)").text(m);
Add 30 days and set the date value to datepicker
Example :
$(document).ready(function() {
var myDate = new Date();
//add a day to the date
myDate.setDate(myDate.getDate() + 30);
var end_date = new Date(myDate.getFullYear(), myDate.getMonth(), myDate.getDate());
$('#datepicker').datepicker({
format: 'dd-mm-yyyy',
orientation: 'bottom'
});
$('#datepicker').datepicker('setDate', end_date);
});
int days = 1;
var newDate = new Date(Date.now() + days*24*60*60*1000);
From How can I add 1 day to current date?
Thanks to Serge