How do i get today's date, 1st day of current month and last day of current month?
see fiddle
var current = new Date(); // this one's pretty self explanatory
console.log('current: ' + current);
var last = new Date();
last.setMonth(last.getMonth() + 1); // go to next month
last.setDate(0); // setting date to 0 is last day of previous month
console.log('last: ' + last);
var first = new Date();
first.setDate(1); // setting date to 1 is first day of current month
console.log('first: ' + first);
Have a look at the Date object: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date
Related
For eg. Date is 2019-01-29 (Jan 29,2019)
I want set month January from 29 Date to 31 Date and display as 2019-01-31 as result using JavaScript
//Set this to whatever date you want..
var d = '2019-02-21';
//Parse out our date object a bit..
var asOf = new Date(d);
var year = asOf.getFullYear();
var month = asOf.getMonth();
//Initially set to first day of next month..
var desiredDate = new Date(year, month + 1, 1);
//Now just subtract 1 day to make it the last day of prior month..
desiredDate.setDate(desiredDate.getDate() - 1);
//Show the date of last day in month..
console.log(`The last day of ${month + 1}/${year} is: ${desiredDate.toLocaleString().split(',')[0]}`);
var date = new Date();
date.setMonth(date.getMonth() + 1);
date.setDate(-1);
Setting date to -1 sets it to the last day of the previous month.
Regards,
Given the current date, I would like to know the last day of the month for the last 24 months. Im trying to get an output similar to this:
30/11/2018 for current month -1
31/10/2018 for current month -2
...
31/12/2016 for current month -24
I've tried searching trough SO but I couldn't find a similar question.
Any help? :)
Edit: Based on Jalees code, I've come with this code, that I think it does what I need:
for(i=0;i<24;i++)
{
d = new Date();
d.setMonth(d.getMonth() + (1 - (i + 1)));
d.setDate(0);
$('#aaaaa').append($('<option>', {value: 'x', text: d.getDate() + '/' + (((d.getMonth() + 1).length == 1) ? '0'+''+(d.getMonth() + 1) : d.getMonth() + 1) + '/' + d.getFullYear()}));
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id="aaaaa"></select>
Use loop index to subtract from current month to build a new date for each month and set the day to zero to get last day of prior month
Example using Array#from():
const curr = new Date();
function getPrevMonth(_,i){
const prev = new Date(curr.getFullYear(), curr.getMonth()-i, 0)
return [prev.getDate(), prev.getMonth()+1, prev.getFullYear()].join('/')// format as desired
}
const months = Array.from({length:24}, getPrevMonth);
console.log(months)
you can use this technique
. for month February you can use 1 and for March 2 and continue.
var month = 0; // January
var d = new Date(2008, month + 1, 0);
alert(d); // last day in January
For example, in the case of 03/27/2016 to 04/02/2016, the dates fall in different months.
var curr = new Date; // get current date
var first = curr.getDate() - curr.getDay();
var last = first + 6; // last day is the first day + 6
var firstday = new Date(curr.setDate(first)).toUTCString();
var lastday = new Date(curr.setDate(last)).toUTCString();
The getDay method returns the number of the day in the week, with Sunday as 0 and Saturday as 6. So if your week starts on Sunday, just subtract the current day number in days from the current date to get the start, and add 6 days get the end, e.g.
function getStartOfWeek(date) {
// Copy date if provided, or use current date if not
date = date? new Date(+date) : new Date();
date.setHours(0,0,0,0);
// Set date to previous Sunday
date.setDate(date.getDate() - date.getDay());
return date;
}
function getEndOfWeek(date) {
date = getStartOfWeek(date);
date.setDate(date.getDate() + 6);
return date;
}
document.write(getStartOfWeek());
document.write('<br>' + getEndOfWeek())
document.write('<br>' + getStartOfWeek(new Date(2016,2,27)))
document.write('<br>' + getEndOfWeek(new Date(2016,2,27)))
You can find below solution for your problem.
let currentDate = new Date; // get current date
let first = currentDate.getDate() - currentDate.getDay();
var last = first + 6; // last day is the first day + 6
let firstDayWeek = new Date(currentDate.setDate(first)).toISOString();
var lastDayWeek = new Date(currentDate.setDate(last)).toISOString();
console.log(firstDayWeek, "first Day in week")
console.log(lastDayWeek, "end Day in week")
I like the moment library for this kind of thing:
moment().startOf("week").toDate();
moment().endOf("week").toDate();
You can try this:
var currDate = new Date();
day = currDate.getDay();
first_day = new Date(currDate.getTime() - 60*60*24* day*1000);
last_day = new Date(currDate.getTime() + 60 * 60 *24 * 6 * 1000);
This is my javascript code:
function borrowbook ()
{
var today = new Date();
var day = today.getDate();
var month = today.getMonth()+1;
var year = today.getFullYear();
var input_day = document.getElementById("textbox").value;
var newday = today.setDate(day + input_day);
var fulltime1 = newday + "-" + month + "-" + year;
alert ("Return Date is: " + fulltime1);
}
And the result was not my expected result:
Actually what I want to do is if a user enters a value in 'Days allowed',I want to display the book return date.But I do not know why does the day of the month cannot show up properly.Any suggestion to solve this problem?
When you do:
var newday = today.setDate(day + input_day);
you are setting the value of newday to the return value of today.setDate(...), which is a time clip.
Since *input_day* is the value of a form control, and such values are always strings, the + operator will concatenate the values, not add them.
What you probably want is the date, so:
today.setDate(day + +input_day); // set the new date, converting input_date to Number
var newday = today.getDate(); // get the new date
Also, you should get the month and year after adding the day as it may change their values:
31 May + 1 day -> 1 June
There are three things you need to change.
Here is a working jsfiddle.
http://jsfiddle.net/bbankes/VMn3x/
First, the month and the year may also be incorrect. If today were 31-Dec 2014, your code would not show 10-Jan 2014, but instead 10-Dec 2013. You can rectify this by getting the day month and the year from the renew date instead of today's date.
Second, input_day is a string, so you need to parse it as an integer using the built-in javascript function parseInt();
Third, the setDate() method on a Date object does not return the new date. This is the problem that RobG shows.
The new function is as follows:
function borrowbook() {
var today = new Date();
var day = today.getDate();
var input_day = document.getElementById("textbox").value;
var returnDate = new Date();
returnDate.setDate(day + parseInt(input_day));
var returnDay = returnDate.getDate();
var returnMonth = returnDate.getMonth() + 1;
var returnYear = returnDate.getFullYear();
var fulltime1 = returnDay + "-" + returnMonth + "-" + returnYear;
alert ("Return Date is: " + fulltime1);
}
Let's say I have 3 variables like this:
var Month = 8; // in reality, it's a parameter provided by some user input
var Year = 2011; // same here
var FirstDay = new Date(Year, Month, 1);
Now I want to have the value of the day before the first day of the month in a variable. I'm doing this:
var LastDayPrevMonth = (FirstDay.getDate() - 1);
It's not working as planned. What the right of doing it?
Thanks.
var LastDayPrevMonth = new Date(Year, Month, 0).getDate();
var LastDayPrevMonth = new Date(FirstDay);
LastDayPrevMonth.setHours(FirstDay.getHours()-24);
var FirstDay = new Date(Year, Month, 1);
var lastMonth = new Date(FirstDay);
lastMonth.setDate(-1);
alert(lastMonth);
And remember that 8 is Sept, not Aug in JavaScript. :)
If you need to calculate this based on today's date ( you want the last day of last month ), the following should help.
If you only care about the month/day/year, this is the simplest and fastest that I can think of:
var d = new Date(); d.setDate(0);
console.log(d);
If you want midnight of last day of the previous month, then:
var d = new Date(); d.setDate(0); d.setHours(0,0,0,0);
console.log(d);
If you want to know the last day of the previous month, based on provided year/month:
var year = 2016, month = 11;
var d = new Date(year, (month - 1)); d.setDate(0); d.setHours(0,0,0,0);
console.log(d);
After running any of the above, to get the YYYY-M-D format:
var str = d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate();
console.log(str);
To see additional methods available, and to see what they do, you can read the docs.
Create a new Date object and pass it the other date coerced to the number of milliseconds since the Unix epoch and then minus a whole day (in milliseconds).
var LastDayPrevMonth = new Date(FirstDay - 864e5);
Example:
var Month = 8; // in reality, it's a parameter provided by some user input
var Year = 2011; // same here
var FirstDay = new Date(Year, Month, 1);
var LastDayPrevMonth = new Date(FirstDay - 864e5);
document.body.innerHTML = LastDayPrevMonth;