Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse DateTime string in JavaScript

Does anyone know how to parse date string in required format dd.mm.yyyy?

like image 763
Azat Avatar asked Oct 16 '09 08:10

Azat


People also ask

How do you convert a string to a Date in JavaScript?

Use the Date() constructor to convert a string to a Date object, e.g. const date = new Date('2022-09-24') . The Date() constructor takes a valid date string as a parameter and returns a Date object. Copied! We used the Date() constructor to convert a string to a Date object.

How convert dd mm yyyy string to Date in JavaScript?

To convert a dd/mm/yyyy string to a date:Pass the year, month minus 1 and the day to the Date() constructor. The Date() constructor creates and returns a new Date object.

How do I convert a string to a Date?

Using strptime() , date and time in string format can be converted to datetime type. The first parameter is the string and the second is the date time format specifier. One advantage of converting to date format is one can select the month or date or time individually.


1 Answers

See:

  • Mozilla Core JavaScript Reference: Date object
  • Mozilla Core JavaScript Reference: String.Split

Code:

var strDate = "03.09.1979"; var dateParts = strDate.split(".");  var date = new Date(dateParts[2], (dateParts[1] - 1), dateParts[0]); 
like image 80
Jonathan Fingland Avatar answered Sep 20 '22 09:09

Jonathan Fingland