0

I have this

let d = new Date("03-08-2018"); //dd-mm-yyyy
console.log(d.getMonth()); // returns 02, I want 07

I have my date in dd-mm-yy. The getMonth() thinks I'm using mm-dd-yy.

How do I get correct month and date.

Nobody
  • 73
  • 11
  • @zer00ne edited my question.. think you didn't understand my question.. The date format I am passing to new Date() is dd-mm-yy. So it's supposed to return 7 when I getMonth() – Nobody Jun 08 '18 at 06:41
  • `new Date(...'03-08-2018'.split('-').reverse().map((v,i)=>v-i%2)).getMonth();` does the trick. ;-) Or you could just do `03-08-2018'.split('-')[1]-1`. – RobG Jun 08 '18 at 07:26

2 Answers2

0

JS Date give the month starts with 0. So Try this below for getting month from date

let d = new Date("03-05-2018"); //dd-mm-yyyy
console.log(d.getMonth()+1);

if you want 4, then you should add +2 ( but i am not sure why you want 4)

let d = new Date("03-05-2018"); //dd-mm-yyyy
console.log(d.getMonth()+2);
Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234
  • i think you didn't understand my question.. The date format I am passing to `new Date()` is `dd-mm-yy`. So it's supposed to return `4` when I `getMonth()` – Nobody Jun 08 '18 at 06:37
0

Your date format is not standard, and I strongly recommend not to use such code on a client web code, because how it behaves would be client-dependent.

EDIT (thx RobG) : Don't use the builtin parser new Date(), this will get you into trouble depending on the client timezone, even if you use a proper format where the month seems to be correctly recognized.

You'll have to create some function to parse the string yourself and create a Date object with year, month and day manually set, that would be the safe way.

Example of manual parsing :

let regex = /^(\d{2})-(\d{2})-(\d{4})$/; // each parsing group is day-month-year
let date = '03-05-2018';

let match = regex.exec(date);
let month = +match[2] - 1; // starting at 0, remove -1 if you want 1-based month number

console.log(month); // returns 4 in this example '03-05-2018'

(of course you should also put some guards if the string is not matching the correct format)

Pac0
  • 21,465
  • 8
  • 65
  • 74
  • hmm.. thought there'd be an easier way..i will wait for some time and if no one answers i will accept yours – Nobody Jun 08 '18 at 06:42
  • yes, good idea to wait for good answers usually on SO. I'm glad I understood your question at last (at the beginning I was like the other answerers thinking you had a 0 vs 1 index-bsaed month problem) – Pac0 Jun 08 '18 at 06:43
  • Re "*…use the ISO standard date format…*". No, don't do that either. ISO format dates are parsed as UTC, so give the wrong date for the period within the host timezone offset of midnight (e.g. a host in UTC-0800 will parse "2018-01-01" as 2017-12-31 local). Just don't use the built-in parser. – RobG Jun 08 '18 at 07:18
  • @RobG I forgot that, indeed it causes problem and gives wrong day (or month) depending on timezone i nthis situation. Removing this part from my answer, thank you. – Pac0 Jun 08 '18 at 07:21