0

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.

2
  • 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. Commented Sep 1, 2016 at 6:37
  • See Calculate last day of month in javascript. Commented Sep 1, 2016 at 6:43

2 Answers 2

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"

Sign up to request clarification or add additional context in comments.

1 Comment

This does not work on FireFox. Chrome Only
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"

2 Comments

I've edited my answer sorry didn't realize it didn't work on safari. This should work cross browser.
Cool. There's no need to include a date argument for new Date, year and month (number) are sufficient. ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.