I have a textbox in which I show only month and year. So when I have June 2013, and when I look for First half, then I want the previous month's date to 14th of June.
So in Javascript I was simply using
var currentmonth=doucument.getElementbyId("textbox");
var currentdate="01-"+currentmonth;// as earlier I had 1st to 15 of month.
I want to get the last months' last date that too in a format, like
"31-May-2013".
I am trying to change like:
currentdate= new Date(currentdate).get...//but not working
Create date object:
var currentdate = new Date("01-"+currentmonth)
Set last day of month using setFullYear, getFullYear and getMonth methods:
currentdate.setFullYear(test.getFullYear(), test.getMonth()+1, 0)
Related
I'd like to be able to get the first and the last date of the current week. For example, this week would be September 4th to September 10th.
The issue I'm running into happens at the end of the month when there are dates from two months (like the last month of August). This caused a problem because the date range was displayed as August 28th to August 3rd when it should've been September 3rd.
I saw some other posts recommending Moment.js, but the Docs say that it shouldn't be used in new projects. What's a good way to do this?
Get the current date: const start = new Date();
Shift that back by the current day-of-week: start.setDate(start.getDate() - start.getDay());
Make a new date based on that: const end = new Date(start);
Shift that date forward: end.setDate(end.getDate() + 6);
That will give you a Sunday to Saturday week. You can shift the days as necessary based on what you consider a "week" to be.
The JavaScript Date API will deal with month shifts automatically. Thus if it's Thursday September 1, moving the day-of-month back to Sunday will correctly give the date in August.
This question already has answers here:
Add one day to date in javascript
(11 answers)
Closed 4 years ago.
I have From-Date, To-Date, and Total-Days...if i have a From-Date and Total-Days then i have to calculate To-Date automatically
I Used The Following method to get the To-Date day
var FromDate = $("#FromDate").val();
var TotalDays = $("#txtDays").val();
FromDate.setDate(parseInt(FromDate.getDate()) + parseInt(NoOfDays));
var dd = FromDate.getDate()-1;
This Will Not Work For Day 1 of every month.....Since It Returns 0
How To Handle This Situation or help me to solve this in another way....Thanx In Advance
try this
var d = new Date('08-20-2018');
d.setDate(d.getDate() - 1);
alert(d.getDate());
Date object is smart enough to know what to do if you set any of the "components" (month, day, hour, minute, second, millisecond) outside of "normal" range - it does the maths for you
If you only need to know the previous date, you can always subtract the 24hours from the epoch time and then convert it back to date object and get the date like:
new Date(new Date('08-01-2018').getTime() - 24*3600000).getDate()
new Date('08-01-2018').getTime() will give you the epoch time of the date you want previous date from
24*3600000 is subtracting the 24 milliseconds from the epoch
After subtracting the value you get the previous day epoch and using the new Date() constructor you are getting the Date object again
On this new date object you can call getDate() method to get the correct result of 31.
I have given date to Date constructor in MM-DD-YYYY format
I need function for getting the start of the same week last year.
For example I have date : 2016-01-10 and it's the first date of the 3d week in 2016, so I need the first day of the 3d week of 2015 and it would be 2015-01-11
(I always need the first day and let's consider that I allways get the first dat of week as param for my function).
I have created such function:
var getTheSameWeekLastYear = function (date) {
var date = moment(date).startOf("week"),
weekNo = date.week();
// move a year ago and set the same week
return date.add(-1, "y").week(weekNo).startOf("week");
}
And it works fine until I got week wich starts in last year and end this year. For example, week#1 2016 - starts 2015-12-27 and ends 2016-01-02.
I this case I am getting thee same week for a year before last year - 2013-12-29.
JSFiddle
What is the best way to solve this problem?
Probably momentjs has any built in function for my task?
I want to count down the days until a particular event using momentjs but I'm getting an unexpected result.
With today's date being 17th April, and the event date being 14th May, I want the resulting number of days to be 27, however my code gives me a result of 57. What's wrong?
function daysRemaining() {
var eventdate = moment([2015, 5, 14]);
var todaysdate = moment();
return eventdate.diff(todaysdate, 'days');
}
alert(daysRemaining());
When creating a moment object using an array, you have to take note that months, hours, minutes, seconds, and milliseconds are all zero indexed. Years and days of the month are 1 indexed. This is to mirror the native Date parameters.
Reference
So either change the month to 4 to reflect May or parse the date as an ISO 8601 string
function daysRemaining() {
var eventdate = moment("2015-05-14");
var todaysdate = moment();
return eventdate.diff(todaysdate, 'days');
}
alert(daysRemaining());
Just to add for anyone else that comes across this - there's actually a helper that does the phrasing etc for you:
https://momentjs.com/docs/#/displaying/to/
/* Retrieve a string describing the time from now to the provided date */
daysUntil: function(dateToCheckAgainst){
return new moment().to(moment(dateToCheckAgainst));
}
// Sample outputs
"in three months"
"in two months"
"in 25 days"
That's because months are zero indexed. So 5 is actually June ;)
Preferred in jQuery, if possible, otherwise any Javascript will work.
I'd like to get the day and week number of month given a specific date or timestamp.
For example
2010-10-26 would return Tuesday and 4 as the week number
2010-11-11 would return Thursday and 2 as the week number
You need to look into the Javascript date object.
To get the day of the week, you need to do something like the following:
var days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
var myDate = new Date('2010-10-26');
var dayOfWeek = days[myDate.getDay()]; // dayOfWeek == 'Tuesday'
This is because getDay() returns a number 0-6 rather than a string. This is useful e.g. for internationalisation.
Your other requirement (week of month) would need custom coding -- it can't be done using the native JS date object.