How can I parse a date with a two-digit year?

2,019 views
Skip to first unread message

Danny Tuppeny

unread,
Aug 25, 2016, 6:46:03 AM8/25/16
to Dart Misc
I've got a string like this that I need to parse:

01/02/16 12:11:12

The 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! :-(

Günter Zöchbauer

unread,
Aug 25, 2016, 7:08:11 AM8/25/16
to Dart Misc
It's better to use a custom parser instead of package:intl
There should be a few similar (closed) issues in the intl github repo with similar answers.

Bigluis Vargas

unread,
Aug 26, 2016, 11:07:01 AM8/26/16
to mi...@dartlang.org
Shouldn't you use yy:

import 'package:intl/intl.dart';

final dateFormat = new DateFormat("d/M/yy H:m:s");

Alan Knight

unread,
Aug 26, 2016, 12:36:35 PM8/26/16
to Dart Misc
"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 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:12

The 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.

Danny Tuppeny

unread,
Aug 26, 2016, 3:31:24 PM8/26/16
to mi...@dartlang.org
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!

Alan Knight

unread,
Aug 26, 2016, 3:59:15 PM8/26/16
to mi...@dartlang.org
On Fri, Aug 26, 2016 at 12:31 PM Danny Tuppeny <da...@tuppeny.com> wrote:
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?
 
Yes, please file 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.

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
 

Thanks!

Danny Tuppeny

unread,
Aug 26, 2016, 4:04:48 PM8/26/16
to mi...@dartlang.org
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/daa484eac791be07f9f9687380c9ed65

I 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 :(

Alan Knight

unread,
Aug 26, 2016, 5:19:12 PM8/26/16
to mi...@dartlang.org
On Fri, Aug 26, 2016 at 1:04 PM Danny Tuppeny <da...@tuppeny.com> wrote:
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.


Thanks.
 


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

I 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 :(

If you don't want two problems, it's also pretty easy to write as a few substring operations.

Parsing a date in any particular well-described format, where we don't have to deal with variations in the input is pretty easy. But there are a lot of formats, and describing the format to the system is almost as much work as extracting the information. Parsing more ambiguous formats gets ugly very quickly and introduces a lot of overhead.

After all, parsing a Date in a well-described format is included. It's in Date.parse. It just isn't the format you want.
Reply all
Reply to author
Forward
0 new messages