how to get first and last date of the current year javascript - javascript

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

Related

How to create a date in Javascript / JQuery from day, month and year?

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)

Looking to add 1 month to current date inside a div

i am looking to display the date plus 1 month inside a div which will be used to display next invoice date. i have seen a few examples in various places, but could not implement. I also saw that there were many solutions, and some controversy surrounding each one.
Once you have a date object, just call setMonth(), passing in the current number of months plus 1.
var CurrentDate = new Date();
CurrentDate.setMonth(CurrentDate.getMonth() + 1);
You can either use this : http://jsfiddle.net/dfA8b/ if you need same date of the next month
var invoiceDt = new Date();
invoiceDt.setMonth(invoiceDt.getMonth()+1);
$('#invoiceDate').text(invoiceDt.toDateString());
Or
You can use this : http://jsfiddle.net/hjSDu/ if you need 30 days month(mostly used for invoice purposes)
var invoiceDt = new Date();
var days = 30;
invoiceDt.setDate(invoiceDt.getDate()+days);
$('#invoiceDate').text(invoiceDt.toDateString());
For the formatting purpose :
http://jsfiddle.net/7bU6n/
var invoiceDt = new Date();
invoiceDt.setMonth(invoiceDt.getMonth()+1);
$('#invoiceDate').text((invoiceDt.getMonth()+1) +"-"+ invoiceDt.getDate() +"-" + invoiceDt.getFullYear());
also see : https://stackoverflow.com/a/1643468/3603806
1 month is not so clear... what You mean? 31, 30 days or if exists simply same date of the following month?
1st case: (assuming 31 days)
var d = new Date(),
dplus31 = d.getDate() + 31;
d.setDate(dplus31);
console.debug(d, dplus31);
2nd case: one month
var d = new Date(),
dplus1 = d.getMonth() + 1;
d.setMonth(dplus1);
console.debug(d, dplus1);
..but even in this case there are some edge cases (ex. 31 January)
hope it helps
function addMonthsNoOverflow(dateParam, intParam) {
var sum = new Date(new Date(dateParam.getTime()).setMonth(dateParam.getMonth() + intParam);
if (sum.getDate() < dateParam.getDate()) { sum.setDate(0); }
return(sum);
}
Notes:
It handles cases where 29, 30 or 31 turned into 1, 2, or 3 by eliminating the overflow
Day of Month is NOT zero-indexed so .setDate(0) is last day of prior month.

Is there a possible way to write current month and year in timeline script?

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 to increment date by giving the number of days in javascript

I want to increment dates using JavaScript I used .setDate(1) to increment dates by one day
but if the date is 31/11/2011 after increment becomes 1/0/2012,
the question is how to increment date by giving the number of days .
js
newDate.setDate(newDate.getDate()+1);
alert(newDate.getFullYear()+"-"+newDate.getMonth()+"-"+newDate.getDate());
That is correct, because in javascript, months are indexed from 0, not 1.
You need to alert like this instead:
alert(newDate.getFullYear()+"-"+(newDate.getMonth()+1)+"-"+newDate.getDate());
That is not wrong, given that months in Javascript dates range from 0 to 11. So when you speak of 31/11/2011, what javascript understands is 31/12/2011.
Lets make it some more clear:
var Date = new Date();
var DaysToAdd = 6;
someDate.setDate(Date.getDate() + DaysToAdd);
Formatting Date to dd/mm/yyyy format:
var dd = Date.getDate();
var mm = Date.getMonth() + 1;
var yyyy = Date.getFullYear();
var NewDate = dd + '/'+ mm + '/'+ yyyy;
Hope this helps.
You can use like this, Suppose you want to increment current date by 2 days then,
var today = new Date(); // Or Date.today()
var newDate = today.add(2).day();

javascript date + 1

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

Categories

Resources