I have a value JUN 16
as string. I need to convert it to a date object. I need to find out last date of the month JUNE year 2016.
Asked
Active
Viewed 2,030 times
0

Cristik
- 30,989
- 25
- 91
- 127

Subin K Kuriakose
- 839
- 7
- 17
-
What have you tried? This isn't a free code writing service, there are many questions and answers here on parsing date strings. Whatever you do, don't parse strings with the Date constructor (or Date.parse, they are equivalent for parsing). You can use a library to parse the string, there are plenty of good ones out there, or write a small (3 line) function to parse the format MMM YY. There are also plenty of questions and good answers on getting the number of days in a month, or setting a date to the last day of the month. Have a go and post what you've tried if you can't work it out. – RobG Sep 01 '16 at 06:37
-
See [*Calculate last day of month in javascript*](http://stackoverflow.com/questions/222309/calculate-last-day-of-month-in-javascript). – RobG Sep 01 '16 at 06:43
2 Answers
1
This is how I am converting a Date("MMM-YY") to Date("YYYY-MM-DD")
Input : Nov-17
run : new Date("Nov-17")
Output : Sat Nov 17 2001 00:00:00 GMT+0530 (India Standard Time)
now by adding a prefix to your date like below
input : 1-Nov-17 //add prefix to your string '1-'
run : new Date("1-Nov-2017")
output: Wed Nov 01 2017 00:00:00 GMT+0530 (India Standard Time)
Moment
run : moment("1-Nov-17").endOf('month').format("YYYY-MM-DD") //End day of the month
output: "2017-11-30"

Sufiyan Ansari
- 1,780
- 20
- 22
0
I have a value "JUN 16" as string .I need to convert to date object
This is how you do it:
var date = new Date(2016, 5, 16);
console.log(date);
i need to find out last date of the month JUNE year 2016
And this is how you can do that:
// 5 is June
var date = new Date(2016, 5 + 1, 0);
console.log(date.getDay())
This tells you that the day is 4
which is "Thursday"

Filip Lauc
- 2,989
- 1
- 17
- 29
-
I've edited my answer sorry didn't realize it didn't work on safari. This should work cross browser. – Filip Lauc Sep 01 '16 at 06:41
-
Cool. There's no need to include a date argument for *new Date*, year and month (number) are sufficient. ;-) – RobG Sep 01 '16 at 06:46