-2

I have used to convert to date format.

var date1 = "28/01/2019"
var d = new Date(date1)
console.log(d)

I am getting invalid date as output. Kindly help

adiga
  • 34,372
  • 9
  • 61
  • 83
Lakshmi G
  • 67
  • 1
  • 1
  • 8
  • 4
    What's in `date1`? – helb Feb 02 '19 at 09:11
  • date1 is a variable where I have stored the actual date of string type – Lakshmi G Feb 02 '19 at 09:12
  • @LakshmiG what they are asking is what is the format of the date string. i.e `04/01/2019` or `Jan 16, 2019 2:00p` etc. – Mark Feb 02 '19 at 09:12
  • this is the value stored in date1 "28/01/2019"; – Lakshmi G Feb 02 '19 at 09:14
  • 3
    Possible duplicate of [How to convert dd/mm/yyyy string into JavaScript Date object?](https://stackoverflow.com/questions/33299687/how-to-convert-dd-mm-yyyy-string-into-javascript-date-object) and [new Date('dd/mm/yyyy') instead of newDate('mm/dd/yyyy')](https://stackoverflow.com/questions/51085176/) and [Convert dd-mm-yyyy string to date](https://stackoverflow.com/questions/7151543) – adiga Feb 02 '19 at 09:25

2 Answers2

6

You need to enter the date in the format of "01/28/2019" for it to be a valid date string which can be parsed. You can do this using .split() to manipulate the string around.

See example below:

var date1 = "28/01/2019".split('/')
var newDate = date1[1] + '/' +date1[0] +'/' +date1[2];

var date = new Date(newDate);
console.log(date);
Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
-1

You can use a technique shown in this answer

var date1 = "28/01/2019".split("/");
var d = new Date(date1[2], date1[1] - 1, date1[0]);

console.log(d);
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79