Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert a date/time string to a DateTime object in Dart?

Tags:

datetime

dart

Say I have a string

"1974-03-20 00:00:00.000" 

It is created using DateTime.now(), how do I convert the string back to a DateTime object?

like image 844
Daniel Mana Avatar asked Mar 20 '18 13:03

Daniel Mana


People also ask

How do I convert a Dart timestamp to DateTime?

DateTime to TImestampThe millisecondsSinceEpoch property of the DateTime class gives us the number of milliseconds since the “Unix epoch” 1970-01-01T00:00:00Z (UTC). This is the timestamp in milliseconds. If you want the timestamp in seconds, just divide the result by 1000.

How do you parse a Dart DateTime?

The DateTime has a static method parse that accepts a subset of ISO 8601 format, not my case. The DateFormat class lets you define the date pattern to parse. I've created the pattern "EEE, dd MMM yyyy hh:mm a zzz". Using it I get a FormatException: Trying to read a from Mon, 11 Aug 2014 12:53 pm PDT at position 23 .


1 Answers

DateTime has a parse method

var parsedDate = DateTime.parse('1974-03-20 00:00:00.000'); 

https://api.dartlang.org/stable/dart-core/DateTime/parse.html

like image 167
Günter Zöchbauer Avatar answered Sep 23 '22 19:09

Günter Zöchbauer