Discussion:
[dart-misc] How can I parse a date with a two-digit year?
Danny Tuppeny
2016-08-25 10:46:03 UTC
Permalink
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+***@dartlang.org.
Günter Zöchbauer
2016-08-25 11:08:10 UTC
Permalink
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.
Post by Danny Tuppeny
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");
}
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+***@dartlang.org.
Bigluis Vargas
2016-08-26 15:07:00 UTC
Permalink
Shouldn't you use *yy*:

import 'package:intl/intl.dart';

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

main() {
date = dateFormat.parse("01/02/16 12:11:12");
}
Post by Danny Tuppeny
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");
}
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+***@dartlang.org.
'Alan Knight' via Dart Misc
2016-08-26 16:36:20 UTC
Permalink
"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.
Post by Danny Tuppeny
import 'package:intl/intl.dart';
final dateFormat = new DateFormat("d/M/yy H:m:s");
main() {
date = dateFormat.parse("01/02/16 12:11:12");
}
Post by Danny Tuppeny
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");
}
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
--
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+***@dartlang.org.
Danny Tuppeny
2016-08-26 19:31:10 UTC
Permalink
Post by 'Alan Knight' via 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.
Should I raise this somewhere?


But the larger issue is that DateFormat is providing a mechanism for
Post by 'Alan Knight' via Dart Misc
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!
--
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+***@dartlang.org.
'Alan Knight' via Dart Misc
2016-08-26 19:59:01 UTC
Permalink
On Fri, 26 Aug 2016 at 17:36 'Alan Knight' via Dart Misc <
Post by 'Alan Knight' via 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.
Should I raise this somewhere?
Yes, please file a bug.
But the larger issue is that DateFormat is providing a mechanism for
Post by 'Alan Knight' via Dart Misc
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!
--
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
--
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+***@dartlang.org.
Danny Tuppeny
2016-08-26 20:04:33 UTC
Permalink
Post by Danny Tuppeny
Should I raise this somewhere?
Yes, please file a bug.
Done!
https://github.com/dart-lang/intl/issues/123


What do you mean by a "custom mechanism"? Performance is not an issue for
Post by Danny Tuppeny
Post by Danny Tuppeny
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 :(
--
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+***@dartlang.org.
'Alan Knight' via Dart Misc
2016-08-26 21:18:58 UTC
Permalink
On Fri, 26 Aug 2016 at 20:59 'Alan Knight' via Dart Misc <
Post by Danny Tuppeny
Should I raise this somewhere?
Yes, please file a bug.
Done!
https://github.com/dart-lang/intl/issues/123
Thanks.
What do you mean by a "custom mechanism"? Performance is not an issue for
Post by Danny Tuppeny
Post by Danny Tuppeny
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.
--
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
--
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+***@dartlang.org.
Loading...