This question already has an answer here:
How to get the integer value of month from moment.js
(1 answer)
Closed 3 years ago.
EDIT: NB: As pointed out in the accepted answer from Zohaib Ijaz month gives the 0-11 value for the month but the second problem is day. day gives a numeric representation of day of the week ie 0 = sunday, 1 = tuesday. If you want to get the 1-31 you need date.
OK I think this must be a super dumb question but I just cannot see it. Today is 11th November 2019
var day = moment().get('day');
var month = moment().get('month');
var year = moment().get('year');
var dateSet = month+"/"+day+"/"+year;
console.log(dateSet);
and I get back: 10/01/2019!
I tried the functional getter of var day = moment().day(); etc. and same again.
I am trying to get today's date and the date in three months times for a datepicker. Greatful for an explanation of this and extremely grateful for a pointer as how to do the +3 months which I tried moment().plus(3,"months") which did not work.
I am extremely tired but I am pretty sure at one stage this was giving the right date. What could possibly have changed?
moment().month() or moment().get('month') will return month from 0 as January to 11 as December. So if you want to create date in MM/DD/YYYY format, user moment().format(format_string). Or add 1 in month while creating your date string. I would suggest to use format and go through moment docs first so you have better idea what it provides out of the box.
See docs
https://momentjs.com/docs/#/get-set/get/
moment().format('MM/DD/YYYY')
Related
So I'm having trouble finding a solution to this online, and if there's one thing I hate; it's working with dates.
I need to be able to calculate a person's age in months and days. So if they were born July 3rd, 2020 I need something like 2 months, 21 days.
I have the code for the months here: return dayjs(dayjs()).diff(value, 'month'); where value is the date, but am struggling to think of a way to get those days to be accurate.
Anything would be great! Thank you.
I would suggest that you subtract the the numbers that represent the days in your dates from one another and take the absolute value.
In your example, with the dates July 3rd, 2020 and September 24th, 2020, this would mean doing 3-24 = -21, which would be 21 after taking the absolute value.
Note that your problem is sort of ill-posed. It doesn't really make sense to give someone's age in months, as the amount of days change in a given month. But what you can do is define that x months later just means changing the value of the months in a date. I.e., two months after July 3rd would be September 3rd. This way you can just take whatever difference remains and use this as number of days, as shown above.
I'm currently just practising JavaScript and I'm trying to create a programme that calculates how many days there are until your next birthday.
I have seen on this site that there is a daysBetween function that I can use to tell the time difference between two dates (I just need to turn these dates into the millisecond value since the 1st Jan 1970).
The problem is that, although one of those dates is the current date (which is easy to convert into its millisecond value), the second is derived from answers the user inputs as a string into a prompt command (there are three different boxes that ask for the year, month and date of their birth). Is there a way I can convert these input strings into a date format that I can then use to find the days between today's date and their next birthday?
Thanks and sorry if this is a stupid question!
Remember in JS that months are indexed from 0, so January === 0.
var date = new Date('2017','5','25'); would be today, assume you have something like:
var userDd = '25';
var userMm = '6'; // User doesn't know the months are indexed at 0.
var userYy = '2017';
var date = new Date(userYy, userMm - 1, userYy);
date.getTime(); // returns the milliseconds value.
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?
Hi I have following code that is suppose to extract day,month and year part separately.
$scope.$watch('inlineDatepicker.date', function() {
alert('hey, myVar has changed!' +$scope.inlineDatepicker.date);
alert($scope.inlineDatepicker.date.getDay());
alert($scope.inlineDatepicker.date.getMonth());
alert($scope.inlineDatepicker.date.getUTCFullYear());
});
Problem with the code is I can extract year correctly but day and month do not extract correctly. I tried as well
alert($scope.inlineDatepicker.date.getUTCDady());
alert($scope.inlineDatepicker.date.getUTCMonth())
Still wrong day and month.
Please let me know how I can change it to get correct month and day values. Thanks. Here is the plunker for it.
http://plnkr.co/edit/8v75gsz8ODUrTfu8S0sh?p=preview
To get day of month, use getUTCDate()
Month is zero based, so 0 means January, 11 December
Sample
$scope.inlineDatepicker.date.getUTCDate(); //prints day of month
$scope.inlineDatepicker.date.getUTCMonth() + 1; //prints month, 0 based, so add 1
$scope.inlineDatepicker.date.getUTCFullYear(); //prints 2015
Get day returns the day of the week. Use getDate() to return the day of the month. If you do getMonth(), january is 0 and december is 11.
You can see the reference documents here: http://www.w3schools.com/jsref/jsref_obj_date.asp
I am facing problem in adding 1 year exact to a date object(from date) and set it as endDate to my to datepicker. It is adding all perfect except months are less by 2. This is the code I have used to add 1 year.
var fromMaximumDate = new Date(Date.parse($('#dtFromDate').val()));
fromMaximumDate.setFullYear(fromMaximumDate.getFullYear() + 1);
fromMaximumDate.setMonth(fromMaximumDate.getMonth());
fromMaximumDate.setDate(fromMaximumDate.getDate());
fromMaximumDate.setHours(fromMaximumDate.getHours());
fromMaximumDate.setMinutes(fromMaximumDate.getMinutes());
fromMaximumDate.setSeconds(fromMaximumDate.getSeconds());.
Image below:
Try the following code:
var fromMaximumDate = new Date(Date.parse($('#dtFromDate').val()));
alert(fromMaximumDate.getMonth());
If you see the month having 2 added to it, then you know that your Date.parse() code is the culprit.
Your code looks right to me. Have a look to this fiddle link.
https://jsfiddle.net/dscfzwx8/
Just remember that int ISO notation 2015 is the year, 05 is the month and 07 is the day