Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse 'Date & Time' string in Javascript which are of custom format

Tags:

I have to parse a date and time string of format "2015-01-16 22:15:00". I want to parse this into JavaScript Date Object. Any help on this?

I tried some jquery plugins, moment.js, date.js, xdate.js. Still no luck.

like image 450
yuva Avatar asked Jan 17 '15 17:01

yuva


People also ask

What does it mean to parse dates?

Parsing dates allows a user to format a Date input from a Form to the local standard format. The Parse Date with Format step converts a String into a DateTime data format. The Get Formatted Date step will convert a DateTime data input into a String.

What is parse date in SQL?

Definition and Usage parse() parses a date string and returns the time difference since January 1, 1970. parse() returns the time difference in milliseconds.

What is parse date in python?

Python has a built-in method to parse dates, strptime . This example takes the string “2020–01–01 14:00” and parses it to a datetime object. The documentation for strptime provides a great overview of all format-string options.

What is parse date in Java?

Use this to parse the string into a Date, and then your other SimpleDateFormat to turn that Date into the format you want. String input = "Thu Jun 18 20:56:02 EDT 2009"; SimpleDateFormat parser = new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy"); Date date = parser.


2 Answers

With moment.js you can create a moment object using the String+Format constructor:

var momentDate = moment('2015-01-16 22:15:00', 'YYYY-MM-DD HH:mm:ss');

Then, you can convert it to JavaScript Date Object using toDate() method:

var jsDate = momentDate.toDate();
like image 70
moises.vilar Avatar answered Oct 20 '22 18:10

moises.vilar


A better solution, I am now using date.js - https://code.google.com/p/datejs/

I included the script in my html page as this -

<script type="text/javascript" src="path/to/date.js"></script>

Then I simply parsed the date string "2015-01-16 22:15:00" with specifying the format as,

var dateString = "2015-01-16 22:15:00";
var date = Date.parse(dateString, "yyyy-MM-dd HH:mm:ss");
like image 42
yuva Avatar answered Oct 20 '22 18:10

yuva