How to add serializer in dart to convert iso 8601 to datetime object?












0














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










share|improve this question



























    0














    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










    share|improve this question

























      0












      0








      0







      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










      share|improve this question













      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 23 '18 at 4:50









      fractal

      9011025




      9011025
























          2 Answers
          2






          active

          oldest

          votes


















          2














          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.






          share|improve this answer





























            0














            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]);





            share|improve this answer





















              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
              });


              }
              });














              draft saved

              draft discarded


















              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









              2














              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.






              share|improve this answer


























                2














                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.






                share|improve this answer
























                  2












                  2








                  2






                  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.






                  share|improve this answer












                  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.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 23 '18 at 6:53









                  lrn

                  9,7491319




                  9,7491319

























                      0














                      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]);





                      share|improve this answer


























                        0














                        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]);





                        share|improve this answer
























                          0












                          0








                          0






                          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]);





                          share|improve this answer












                          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]);






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 30 '18 at 17:01









                          Jimson

                          263




                          263






























                              draft saved

                              draft discarded




















































                              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.




                              draft saved


                              draft discarded














                              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





















































                              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







                              Popular posts from this blog

                              What visual should I use to simply compare current year value vs last year in Power BI desktop

                              Alexandru Averescu

                              Trompette piccolo