01/02/16 12:11:12
import 'package:intl/intl.dart';final dateFormat = new DateFormat("d/M/y H:m:s");main() {date = dateFormat.parse("01/02/16 12:11:12");}
import 'package:intl/intl.dart';
final dateFormat = new DateFormat("d/M/yy H:m:s");
On Thursday, August 25, 2016 at 6:46:03 AM UTC-4, Danny Tuppeny wrote:
I've got a string like this that I need to parse:01/02/16 12:11:12The format is dd/MM/yy hh:mm:ss. I'm parsing it like this:
import 'package:intl/intl.dart';final dateFormat = new DateFormat("d/M/y H:m:s");
main() {date = dateFormat.parse("01/02/16 12:11:12");}
However the year comes out at 0016. I can't find any option to parse a two digit year, I can't manipulate the year property of what comes back (year on DateTime is readonly) and the add method takes a duration which only accepts days, no years! :-(
--
For other discussions, see https://groups.google.com/a/dartlang.org/
For HOWTO questions, visit http://stackoverflow.com/tags/dart
To file a bug report or feature request, go to http://www.dartbug.com/new
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+uns...@dartlang.org.
"yy" probably ought to parse 2-digit years correctly, as that's what it produces. But it doesn't seem to, so that looks like a bug.
But the larger issue is that DateFormat is providing a mechanism for locale-dependent parsing of dates and times. If you just want to parse a fixed format it will be faster and more reliable to use a custom mechanism.
On Fri, 26 Aug 2016 at 17:36 'Alan Knight' via Dart Misc <mi...@dartlang.org> wrote:"yy" probably ought to parse 2-digit years correctly, as that's what it produces. But it doesn't seem to, so that looks like a bug.Should I raise this somewhere?
But the larger issue is that DateFormat is providing a mechanism for locale-dependent parsing of dates and times. If you just want to parse a fixed format it will be faster and more reliable to use a custom mechanism.What do you mean by a "custom mechanism"? Performance is not an issue for this little script, but I'd be interested to know the recommended way of parsing a date in a known format.
Thanks!
Should I raise this somewhere?Yes, please file a bug.
What do you mean by a "custom mechanism"? Performance is not an issue for this little script, but I'd be interested to know the recommended way of parsing a date in a known format.There are probably lots of libraries available, but if you have a strictly fixed format, e.g. coming from a log or a known API, where you don't have to worry about the number of digits or invalid formats then it's pretty easy to just read the numbers out of it. e.g. https://dartpad.dartlang.org/daa484eac791be07f9f9687380c9ed65
On Fri, 26 Aug 2016 at 20:59 'Alan Knight' via Dart Misc <mi...@dartlang.org> wrote:Should I raise this somewhere?Yes, please file a bug.
What do you mean by a "custom mechanism"? Performance is not an issue for this little script, but I'd be interested to know the recommended way of parsing a date in a known format.There are probably lots of libraries available, but if you have a strictly fixed format, e.g. coming from a log or a known API, where you don't have to worry about the number of digits or invalid formats then it's pretty easy to just read the numbers out of it. e.g. https://dartpad.dartlang.org/daa484eac791be07f9f9687380c9ed65I originally wrote a regex and then I thought "what am I doing, this is crazy, I only want to parse a date in a well described format"! I figured date would've been one of the included batteries :(