Why DateTime.new returns year only?












0















I want to seed DateTime in seeds.rb from YAML file



this is code from seeds.rb



created_at: DateTime.new("#{post['created_at']}".to_i)


and in posts YAML file:



created_at: 2010-04-16


Output of this is created_at: "2010-01-01 00:00:00"



Question is: In what format should be created_at in YAML file?










share|improve this question





























    0















    I want to seed DateTime in seeds.rb from YAML file



    this is code from seeds.rb



    created_at: DateTime.new("#{post['created_at']}".to_i)


    and in posts YAML file:



    created_at: 2010-04-16


    Output of this is created_at: "2010-01-01 00:00:00"



    Question is: In what format should be created_at in YAML file?










    share|improve this question



























      0












      0








      0








      I want to seed DateTime in seeds.rb from YAML file



      this is code from seeds.rb



      created_at: DateTime.new("#{post['created_at']}".to_i)


      and in posts YAML file:



      created_at: 2010-04-16


      Output of this is created_at: "2010-01-01 00:00:00"



      Question is: In what format should be created_at in YAML file?










      share|improve this question
















      I want to seed DateTime in seeds.rb from YAML file



      this is code from seeds.rb



      created_at: DateTime.new("#{post['created_at']}".to_i)


      and in posts YAML file:



      created_at: 2010-04-16


      Output of this is created_at: "2010-01-01 00:00:00"



      Question is: In what format should be created_at in YAML file?







      ruby-on-rails ruby date yaml seed






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 '18 at 12:22









      Anthon

      30.7k1795148




      30.7k1795148










      asked Nov 22 '18 at 11:43









      pfcpfc

      235




      235
























          2 Answers
          2






          active

          oldest

          votes


















          2














          Use DateTime#parse:



          DateTime.parse('2010-04-16')
          #⇒ Fri, 16 Apr 2010 00:00:00 +0000


          For your example:



          created_at: DateTime.parse(post['created_at'])




          If post['created_at'] is already an instance of DateTime (e.g. loaded with YAML,) just assign it as is:



          created_at: post['created_at']





          share|improve this answer


























          • got an error: TypeError: no implicit conversion of DateTime into String

            – pfc
            Nov 22 '18 at 13:52











          • That error message most likely means post['created_at'] is already a DateTime instance and you don’t need to do anything with it.

            – Aleksei Matiushkin
            Nov 22 '18 at 13:53











          • yes, actually I didn't need to add DateTime.parse nor DateTime.new, just post['created_at'] works :) thanks

            – pfc
            Nov 22 '18 at 14:03



















          0














          When you check "2010-04-16".to_i then you get 2010. So you call DateTime.new(2010) and get the result you see.



          You can't use a string itself, with DateTime.new("2010-04-16") you get a type error.



          But Yaml converts already to a Date when it parses 2010-04-16, so I guess you can use post['created_at'].to_datetime



          Full raw ruby example:



          require 'yaml'
          require 'date'
          post = YAML.load('created_at: 2010-04-16')
          p post['created_at'] #-> #<Date: 2010-04-16 ((2455303j,0s,0n),+0s,2299161j)>
          p post['created_at'].to_datetime #-> #<DateTime: 2010-04-16T00:00:00+00:00 ((2455303j,0s,0n),+0s,2299161j)>


          Your seeds.rb may look like



          created_at: post['created_at'].to_datetime





          share|improve this answer


























          • I got an ArgumentError: comparison of DateTime with 0 failed

            – pfc
            Nov 22 '18 at 13:47











          • DateTime.new in the last line looks a bit redundant :)

            – Aleksei Matiushkin
            Nov 22 '18 at 13:54











          • @AlekseiMatiushkin You are right ;) I corrected it

            – knut
            Nov 22 '18 at 22:13











          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%2f53430270%2fwhy-datetime-new-returns-year-only%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














          Use DateTime#parse:



          DateTime.parse('2010-04-16')
          #⇒ Fri, 16 Apr 2010 00:00:00 +0000


          For your example:



          created_at: DateTime.parse(post['created_at'])




          If post['created_at'] is already an instance of DateTime (e.g. loaded with YAML,) just assign it as is:



          created_at: post['created_at']





          share|improve this answer


























          • got an error: TypeError: no implicit conversion of DateTime into String

            – pfc
            Nov 22 '18 at 13:52











          • That error message most likely means post['created_at'] is already a DateTime instance and you don’t need to do anything with it.

            – Aleksei Matiushkin
            Nov 22 '18 at 13:53











          • yes, actually I didn't need to add DateTime.parse nor DateTime.new, just post['created_at'] works :) thanks

            – pfc
            Nov 22 '18 at 14:03
















          2














          Use DateTime#parse:



          DateTime.parse('2010-04-16')
          #⇒ Fri, 16 Apr 2010 00:00:00 +0000


          For your example:



          created_at: DateTime.parse(post['created_at'])




          If post['created_at'] is already an instance of DateTime (e.g. loaded with YAML,) just assign it as is:



          created_at: post['created_at']





          share|improve this answer


























          • got an error: TypeError: no implicit conversion of DateTime into String

            – pfc
            Nov 22 '18 at 13:52











          • That error message most likely means post['created_at'] is already a DateTime instance and you don’t need to do anything with it.

            – Aleksei Matiushkin
            Nov 22 '18 at 13:53











          • yes, actually I didn't need to add DateTime.parse nor DateTime.new, just post['created_at'] works :) thanks

            – pfc
            Nov 22 '18 at 14:03














          2












          2








          2







          Use DateTime#parse:



          DateTime.parse('2010-04-16')
          #⇒ Fri, 16 Apr 2010 00:00:00 +0000


          For your example:



          created_at: DateTime.parse(post['created_at'])




          If post['created_at'] is already an instance of DateTime (e.g. loaded with YAML,) just assign it as is:



          created_at: post['created_at']





          share|improve this answer















          Use DateTime#parse:



          DateTime.parse('2010-04-16')
          #⇒ Fri, 16 Apr 2010 00:00:00 +0000


          For your example:



          created_at: DateTime.parse(post['created_at'])




          If post['created_at'] is already an instance of DateTime (e.g. loaded with YAML,) just assign it as is:



          created_at: post['created_at']






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 22 '18 at 13:55

























          answered Nov 22 '18 at 12:26









          Aleksei MatiushkinAleksei Matiushkin

          82.9k95692




          82.9k95692













          • got an error: TypeError: no implicit conversion of DateTime into String

            – pfc
            Nov 22 '18 at 13:52











          • That error message most likely means post['created_at'] is already a DateTime instance and you don’t need to do anything with it.

            – Aleksei Matiushkin
            Nov 22 '18 at 13:53











          • yes, actually I didn't need to add DateTime.parse nor DateTime.new, just post['created_at'] works :) thanks

            – pfc
            Nov 22 '18 at 14:03



















          • got an error: TypeError: no implicit conversion of DateTime into String

            – pfc
            Nov 22 '18 at 13:52











          • That error message most likely means post['created_at'] is already a DateTime instance and you don’t need to do anything with it.

            – Aleksei Matiushkin
            Nov 22 '18 at 13:53











          • yes, actually I didn't need to add DateTime.parse nor DateTime.new, just post['created_at'] works :) thanks

            – pfc
            Nov 22 '18 at 14:03

















          got an error: TypeError: no implicit conversion of DateTime into String

          – pfc
          Nov 22 '18 at 13:52





          got an error: TypeError: no implicit conversion of DateTime into String

          – pfc
          Nov 22 '18 at 13:52













          That error message most likely means post['created_at'] is already a DateTime instance and you don’t need to do anything with it.

          – Aleksei Matiushkin
          Nov 22 '18 at 13:53





          That error message most likely means post['created_at'] is already a DateTime instance and you don’t need to do anything with it.

          – Aleksei Matiushkin
          Nov 22 '18 at 13:53













          yes, actually I didn't need to add DateTime.parse nor DateTime.new, just post['created_at'] works :) thanks

          – pfc
          Nov 22 '18 at 14:03





          yes, actually I didn't need to add DateTime.parse nor DateTime.new, just post['created_at'] works :) thanks

          – pfc
          Nov 22 '18 at 14:03













          0














          When you check "2010-04-16".to_i then you get 2010. So you call DateTime.new(2010) and get the result you see.



          You can't use a string itself, with DateTime.new("2010-04-16") you get a type error.



          But Yaml converts already to a Date when it parses 2010-04-16, so I guess you can use post['created_at'].to_datetime



          Full raw ruby example:



          require 'yaml'
          require 'date'
          post = YAML.load('created_at: 2010-04-16')
          p post['created_at'] #-> #<Date: 2010-04-16 ((2455303j,0s,0n),+0s,2299161j)>
          p post['created_at'].to_datetime #-> #<DateTime: 2010-04-16T00:00:00+00:00 ((2455303j,0s,0n),+0s,2299161j)>


          Your seeds.rb may look like



          created_at: post['created_at'].to_datetime





          share|improve this answer


























          • I got an ArgumentError: comparison of DateTime with 0 failed

            – pfc
            Nov 22 '18 at 13:47











          • DateTime.new in the last line looks a bit redundant :)

            – Aleksei Matiushkin
            Nov 22 '18 at 13:54











          • @AlekseiMatiushkin You are right ;) I corrected it

            – knut
            Nov 22 '18 at 22:13
















          0














          When you check "2010-04-16".to_i then you get 2010. So you call DateTime.new(2010) and get the result you see.



          You can't use a string itself, with DateTime.new("2010-04-16") you get a type error.



          But Yaml converts already to a Date when it parses 2010-04-16, so I guess you can use post['created_at'].to_datetime



          Full raw ruby example:



          require 'yaml'
          require 'date'
          post = YAML.load('created_at: 2010-04-16')
          p post['created_at'] #-> #<Date: 2010-04-16 ((2455303j,0s,0n),+0s,2299161j)>
          p post['created_at'].to_datetime #-> #<DateTime: 2010-04-16T00:00:00+00:00 ((2455303j,0s,0n),+0s,2299161j)>


          Your seeds.rb may look like



          created_at: post['created_at'].to_datetime





          share|improve this answer


























          • I got an ArgumentError: comparison of DateTime with 0 failed

            – pfc
            Nov 22 '18 at 13:47











          • DateTime.new in the last line looks a bit redundant :)

            – Aleksei Matiushkin
            Nov 22 '18 at 13:54











          • @AlekseiMatiushkin You are right ;) I corrected it

            – knut
            Nov 22 '18 at 22:13














          0












          0








          0







          When you check "2010-04-16".to_i then you get 2010. So you call DateTime.new(2010) and get the result you see.



          You can't use a string itself, with DateTime.new("2010-04-16") you get a type error.



          But Yaml converts already to a Date when it parses 2010-04-16, so I guess you can use post['created_at'].to_datetime



          Full raw ruby example:



          require 'yaml'
          require 'date'
          post = YAML.load('created_at: 2010-04-16')
          p post['created_at'] #-> #<Date: 2010-04-16 ((2455303j,0s,0n),+0s,2299161j)>
          p post['created_at'].to_datetime #-> #<DateTime: 2010-04-16T00:00:00+00:00 ((2455303j,0s,0n),+0s,2299161j)>


          Your seeds.rb may look like



          created_at: post['created_at'].to_datetime





          share|improve this answer















          When you check "2010-04-16".to_i then you get 2010. So you call DateTime.new(2010) and get the result you see.



          You can't use a string itself, with DateTime.new("2010-04-16") you get a type error.



          But Yaml converts already to a Date when it parses 2010-04-16, so I guess you can use post['created_at'].to_datetime



          Full raw ruby example:



          require 'yaml'
          require 'date'
          post = YAML.load('created_at: 2010-04-16')
          p post['created_at'] #-> #<Date: 2010-04-16 ((2455303j,0s,0n),+0s,2299161j)>
          p post['created_at'].to_datetime #-> #<DateTime: 2010-04-16T00:00:00+00:00 ((2455303j,0s,0n),+0s,2299161j)>


          Your seeds.rb may look like



          created_at: post['created_at'].to_datetime






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 22 '18 at 22:11

























          answered Nov 22 '18 at 12:40









          knutknut

          22.4k46597




          22.4k46597













          • I got an ArgumentError: comparison of DateTime with 0 failed

            – pfc
            Nov 22 '18 at 13:47











          • DateTime.new in the last line looks a bit redundant :)

            – Aleksei Matiushkin
            Nov 22 '18 at 13:54











          • @AlekseiMatiushkin You are right ;) I corrected it

            – knut
            Nov 22 '18 at 22:13



















          • I got an ArgumentError: comparison of DateTime with 0 failed

            – pfc
            Nov 22 '18 at 13:47











          • DateTime.new in the last line looks a bit redundant :)

            – Aleksei Matiushkin
            Nov 22 '18 at 13:54











          • @AlekseiMatiushkin You are right ;) I corrected it

            – knut
            Nov 22 '18 at 22:13

















          I got an ArgumentError: comparison of DateTime with 0 failed

          – pfc
          Nov 22 '18 at 13:47





          I got an ArgumentError: comparison of DateTime with 0 failed

          – pfc
          Nov 22 '18 at 13:47













          DateTime.new in the last line looks a bit redundant :)

          – Aleksei Matiushkin
          Nov 22 '18 at 13:54





          DateTime.new in the last line looks a bit redundant :)

          – Aleksei Matiushkin
          Nov 22 '18 at 13:54













          @AlekseiMatiushkin You are right ;) I corrected it

          – knut
          Nov 22 '18 at 22:13





          @AlekseiMatiushkin You are right ;) I corrected it

          – knut
          Nov 22 '18 at 22:13


















          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.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53430270%2fwhy-datetime-new-returns-year-only%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

          MongoDB - Not Authorized To Execute Command

          How to fix TextFormField cause rebuild widget in Flutter

          in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith