How to add serializer in dart to convert iso 8601 to datetime object?
In dart I want to do this:
var s = "2018-11-23T04:25:41.9241411Z"; // string comes from a json but represented here for simplicity like this
var d = DateTime.parse(s);
but it throws a null.
Dart can't seem to parse iso 8601 date time formats. I've found a custom serializer called "Iso8601DateTimeSerializer" but how do I add it to my flutter app?
links: https://reviewable.io/reviews/google/built_value.dart/429#-
The instructions here only indicate adding it to dart using "SerializersBuilder.add" but I'm a newbie and cant find out how?
link:
https://pub.dartlang.org/documentation/built_value/latest/iso_8601_date_time_serializer/Iso8601DateTimeSerializer-class.html
dart flutter datetime-format iso8601
add a comment |
In dart I want to do this:
var s = "2018-11-23T04:25:41.9241411Z"; // string comes from a json but represented here for simplicity like this
var d = DateTime.parse(s);
but it throws a null.
Dart can't seem to parse iso 8601 date time formats. I've found a custom serializer called "Iso8601DateTimeSerializer" but how do I add it to my flutter app?
links: https://reviewable.io/reviews/google/built_value.dart/429#-
The instructions here only indicate adding it to dart using "SerializersBuilder.add" but I'm a newbie and cant find out how?
link:
https://pub.dartlang.org/documentation/built_value/latest/iso_8601_date_time_serializer/Iso8601DateTimeSerializer-class.html
dart flutter datetime-format iso8601
add a comment |
In dart I want to do this:
var s = "2018-11-23T04:25:41.9241411Z"; // string comes from a json but represented here for simplicity like this
var d = DateTime.parse(s);
but it throws a null.
Dart can't seem to parse iso 8601 date time formats. I've found a custom serializer called "Iso8601DateTimeSerializer" but how do I add it to my flutter app?
links: https://reviewable.io/reviews/google/built_value.dart/429#-
The instructions here only indicate adding it to dart using "SerializersBuilder.add" but I'm a newbie and cant find out how?
link:
https://pub.dartlang.org/documentation/built_value/latest/iso_8601_date_time_serializer/Iso8601DateTimeSerializer-class.html
dart flutter datetime-format iso8601
In dart I want to do this:
var s = "2018-11-23T04:25:41.9241411Z"; // string comes from a json but represented here for simplicity like this
var d = DateTime.parse(s);
but it throws a null.
Dart can't seem to parse iso 8601 date time formats. I've found a custom serializer called "Iso8601DateTimeSerializer" but how do I add it to my flutter app?
links: https://reviewable.io/reviews/google/built_value.dart/429#-
The instructions here only indicate adding it to dart using "SerializersBuilder.add" but I'm a newbie and cant find out how?
link:
https://pub.dartlang.org/documentation/built_value/latest/iso_8601_date_time_serializer/Iso8601DateTimeSerializer-class.html
dart flutter datetime-format iso8601
dart flutter datetime-format iso8601
asked Nov 23 '18 at 4:50
fractal
9011025
9011025
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
The problem is that Dart's DateTime.parse
only accepts up to six digits of fractional seconds, and your input has seven.
... and then optionally a '.' followed by a one-to-six digit second fraction.
You can sanitize your input down to six digits using something like:
String restrictFractionalSeconds(String dateTime) =>
dateTime.replaceFirstMapped(RegExp("(.d{6})d+"), (m) => m[1]);
Maybe the parse function should just accept more digits, even if they don't affect the value.
add a comment |
Just to add to Irn's answer. You need to add some escapes for the regex to work properly.
String restrictFractionalSeconds(String dateTime) =>
dateTime.replaceFirstMapped(RegExp("(\.\d{6})\d+"), (m) => m[1]);
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53440796%2fhow-to-add-serializer-in-dart-to-convert-iso-8601-to-datetime-object%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
The problem is that Dart's DateTime.parse
only accepts up to six digits of fractional seconds, and your input has seven.
... and then optionally a '.' followed by a one-to-six digit second fraction.
You can sanitize your input down to six digits using something like:
String restrictFractionalSeconds(String dateTime) =>
dateTime.replaceFirstMapped(RegExp("(.d{6})d+"), (m) => m[1]);
Maybe the parse function should just accept more digits, even if they don't affect the value.
add a comment |
The problem is that Dart's DateTime.parse
only accepts up to six digits of fractional seconds, and your input has seven.
... and then optionally a '.' followed by a one-to-six digit second fraction.
You can sanitize your input down to six digits using something like:
String restrictFractionalSeconds(String dateTime) =>
dateTime.replaceFirstMapped(RegExp("(.d{6})d+"), (m) => m[1]);
Maybe the parse function should just accept more digits, even if they don't affect the value.
add a comment |
The problem is that Dart's DateTime.parse
only accepts up to six digits of fractional seconds, and your input has seven.
... and then optionally a '.' followed by a one-to-six digit second fraction.
You can sanitize your input down to six digits using something like:
String restrictFractionalSeconds(String dateTime) =>
dateTime.replaceFirstMapped(RegExp("(.d{6})d+"), (m) => m[1]);
Maybe the parse function should just accept more digits, even if they don't affect the value.
The problem is that Dart's DateTime.parse
only accepts up to six digits of fractional seconds, and your input has seven.
... and then optionally a '.' followed by a one-to-six digit second fraction.
You can sanitize your input down to six digits using something like:
String restrictFractionalSeconds(String dateTime) =>
dateTime.replaceFirstMapped(RegExp("(.d{6})d+"), (m) => m[1]);
Maybe the parse function should just accept more digits, even if they don't affect the value.
answered Nov 23 '18 at 6:53
lrn
9,7491319
9,7491319
add a comment |
add a comment |
Just to add to Irn's answer. You need to add some escapes for the regex to work properly.
String restrictFractionalSeconds(String dateTime) =>
dateTime.replaceFirstMapped(RegExp("(\.\d{6})\d+"), (m) => m[1]);
add a comment |
Just to add to Irn's answer. You need to add some escapes for the regex to work properly.
String restrictFractionalSeconds(String dateTime) =>
dateTime.replaceFirstMapped(RegExp("(\.\d{6})\d+"), (m) => m[1]);
add a comment |
Just to add to Irn's answer. You need to add some escapes for the regex to work properly.
String restrictFractionalSeconds(String dateTime) =>
dateTime.replaceFirstMapped(RegExp("(\.\d{6})\d+"), (m) => m[1]);
Just to add to Irn's answer. You need to add some escapes for the regex to work properly.
String restrictFractionalSeconds(String dateTime) =>
dateTime.replaceFirstMapped(RegExp("(\.\d{6})\d+"), (m) => m[1]);
answered Nov 30 '18 at 17:01
Jimson
263
263
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53440796%2fhow-to-add-serializer-in-dart-to-convert-iso-8601-to-datetime-object%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown