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

      29.2k1693145




      29.2k1693145










      asked Nov 22 '18 at 11:43









      pfcpfc

      113




      113
























          3 Answers
          3






          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














          datetime format will help you.






          share|improve this answer
























          • Seriously? Crappy Rails helper to parse a date from a string?

            – Aleksei Matiushkin
            Nov 22 '18 at 12:29











          • @AlekseiMatiushkin Please suggest then better one.

            – ray
            Nov 23 '18 at 7:13











          • Looks like I did in the answer that was accepted.

            – Aleksei Matiushkin
            Nov 23 '18 at 7:15











          • @AlekseiMatiushkin Yeah, it should be like that :) good one.

            – ray
            Nov 23 '18 at 7:18



















          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

























          3 Answers
          3






          active

          oldest

          votes








          3 Answers
          3






          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

          80.2k95190




          80.2k95190













          • 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














          datetime format will help you.






          share|improve this answer
























          • Seriously? Crappy Rails helper to parse a date from a string?

            – Aleksei Matiushkin
            Nov 22 '18 at 12:29











          • @AlekseiMatiushkin Please suggest then better one.

            – ray
            Nov 23 '18 at 7:13











          • Looks like I did in the answer that was accepted.

            – Aleksei Matiushkin
            Nov 23 '18 at 7:15











          • @AlekseiMatiushkin Yeah, it should be like that :) good one.

            – ray
            Nov 23 '18 at 7:18
















          0














          datetime format will help you.






          share|improve this answer
























          • Seriously? Crappy Rails helper to parse a date from a string?

            – Aleksei Matiushkin
            Nov 22 '18 at 12:29











          • @AlekseiMatiushkin Please suggest then better one.

            – ray
            Nov 23 '18 at 7:13











          • Looks like I did in the answer that was accepted.

            – Aleksei Matiushkin
            Nov 23 '18 at 7:15











          • @AlekseiMatiushkin Yeah, it should be like that :) good one.

            – ray
            Nov 23 '18 at 7:18














          0












          0








          0







          datetime format will help you.






          share|improve this answer













          datetime format will help you.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 22 '18 at 11:49









          rayray

          1,8141319




          1,8141319













          • Seriously? Crappy Rails helper to parse a date from a string?

            – Aleksei Matiushkin
            Nov 22 '18 at 12:29











          • @AlekseiMatiushkin Please suggest then better one.

            – ray
            Nov 23 '18 at 7:13











          • Looks like I did in the answer that was accepted.

            – Aleksei Matiushkin
            Nov 23 '18 at 7:15











          • @AlekseiMatiushkin Yeah, it should be like that :) good one.

            – ray
            Nov 23 '18 at 7:18



















          • Seriously? Crappy Rails helper to parse a date from a string?

            – Aleksei Matiushkin
            Nov 22 '18 at 12:29











          • @AlekseiMatiushkin Please suggest then better one.

            – ray
            Nov 23 '18 at 7:13











          • Looks like I did in the answer that was accepted.

            – Aleksei Matiushkin
            Nov 23 '18 at 7:15











          • @AlekseiMatiushkin Yeah, it should be like that :) good one.

            – ray
            Nov 23 '18 at 7:18

















          Seriously? Crappy Rails helper to parse a date from a string?

          – Aleksei Matiushkin
          Nov 22 '18 at 12:29





          Seriously? Crappy Rails helper to parse a date from a string?

          – Aleksei Matiushkin
          Nov 22 '18 at 12:29













          @AlekseiMatiushkin Please suggest then better one.

          – ray
          Nov 23 '18 at 7:13





          @AlekseiMatiushkin Please suggest then better one.

          – ray
          Nov 23 '18 at 7:13













          Looks like I did in the answer that was accepted.

          – Aleksei Matiushkin
          Nov 23 '18 at 7:15





          Looks like I did in the answer that was accepted.

          – Aleksei Matiushkin
          Nov 23 '18 at 7:15













          @AlekseiMatiushkin Yeah, it should be like that :) good one.

          – ray
          Nov 23 '18 at 7:18





          @AlekseiMatiushkin Yeah, it should be like that :) good one.

          – ray
          Nov 23 '18 at 7:18











          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.2k46297




          22.2k46297













          • 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

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          TypeError: fit_transform() missing 1 required positional argument: 'X'