Why DateTime.new returns year only?
up vote
0
down vote
favorite
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
add a comment |
up vote
0
down vote
favorite
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
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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
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
ruby-on-rails ruby date yaml seed
edited Nov 22 at 12:22
Anthon
28.2k1693144
28.2k1693144
asked Nov 22 at 11:43
pfc
102
102
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
up vote
2
down vote
accepted
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']
got an error: TypeError: no implicit conversion of DateTime into String
– pfc
Nov 22 at 13:52
That error message most likely meanspost['created_at']
is already aDateTime
instance and you don’t need to do anything with it.
– Aleksei Matiushkin
Nov 22 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 at 14:03
add a comment |
up vote
0
down vote
datetime format will help you.
Seriously? Crappy Rails helper to parse a date from a string?
– Aleksei Matiushkin
Nov 22 at 12:29
@AlekseiMatiushkin Please suggest then better one.
– ray
Nov 23 at 7:13
Looks like I did in the answer that was accepted.
– Aleksei Matiushkin
Nov 23 at 7:15
@AlekseiMatiushkin Yeah, it should be like that :) good one.
– ray
Nov 23 at 7:18
add a comment |
up vote
0
down vote
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
I got an ArgumentError: comparison of DateTime with 0 failed
– pfc
Nov 22 at 13:47
DateTime.new
in the last line looks a bit redundant :)
– Aleksei Matiushkin
Nov 22 at 13:54
@AlekseiMatiushkin You are right ;) I corrected it
– knut
Nov 22 at 22:13
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',
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%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
up vote
2
down vote
accepted
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']
got an error: TypeError: no implicit conversion of DateTime into String
– pfc
Nov 22 at 13:52
That error message most likely meanspost['created_at']
is already aDateTime
instance and you don’t need to do anything with it.
– Aleksei Matiushkin
Nov 22 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 at 14:03
add a comment |
up vote
2
down vote
accepted
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']
got an error: TypeError: no implicit conversion of DateTime into String
– pfc
Nov 22 at 13:52
That error message most likely meanspost['created_at']
is already aDateTime
instance and you don’t need to do anything with it.
– Aleksei Matiushkin
Nov 22 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 at 14:03
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
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']
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']
edited Nov 22 at 13:55
answered Nov 22 at 12:26
Aleksei Matiushkin
78k95190
78k95190
got an error: TypeError: no implicit conversion of DateTime into String
– pfc
Nov 22 at 13:52
That error message most likely meanspost['created_at']
is already aDateTime
instance and you don’t need to do anything with it.
– Aleksei Matiushkin
Nov 22 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 at 14:03
add a comment |
got an error: TypeError: no implicit conversion of DateTime into String
– pfc
Nov 22 at 13:52
That error message most likely meanspost['created_at']
is already aDateTime
instance and you don’t need to do anything with it.
– Aleksei Matiushkin
Nov 22 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 at 14:03
got an error: TypeError: no implicit conversion of DateTime into String
– pfc
Nov 22 at 13:52
got an error: TypeError: no implicit conversion of DateTime into String
– pfc
Nov 22 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 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 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 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 at 14:03
add a comment |
up vote
0
down vote
datetime format will help you.
Seriously? Crappy Rails helper to parse a date from a string?
– Aleksei Matiushkin
Nov 22 at 12:29
@AlekseiMatiushkin Please suggest then better one.
– ray
Nov 23 at 7:13
Looks like I did in the answer that was accepted.
– Aleksei Matiushkin
Nov 23 at 7:15
@AlekseiMatiushkin Yeah, it should be like that :) good one.
– ray
Nov 23 at 7:18
add a comment |
up vote
0
down vote
datetime format will help you.
Seriously? Crappy Rails helper to parse a date from a string?
– Aleksei Matiushkin
Nov 22 at 12:29
@AlekseiMatiushkin Please suggest then better one.
– ray
Nov 23 at 7:13
Looks like I did in the answer that was accepted.
– Aleksei Matiushkin
Nov 23 at 7:15
@AlekseiMatiushkin Yeah, it should be like that :) good one.
– ray
Nov 23 at 7:18
add a comment |
up vote
0
down vote
up vote
0
down vote
datetime format will help you.
datetime format will help you.
answered Nov 22 at 11:49
ray
71912
71912
Seriously? Crappy Rails helper to parse a date from a string?
– Aleksei Matiushkin
Nov 22 at 12:29
@AlekseiMatiushkin Please suggest then better one.
– ray
Nov 23 at 7:13
Looks like I did in the answer that was accepted.
– Aleksei Matiushkin
Nov 23 at 7:15
@AlekseiMatiushkin Yeah, it should be like that :) good one.
– ray
Nov 23 at 7:18
add a comment |
Seriously? Crappy Rails helper to parse a date from a string?
– Aleksei Matiushkin
Nov 22 at 12:29
@AlekseiMatiushkin Please suggest then better one.
– ray
Nov 23 at 7:13
Looks like I did in the answer that was accepted.
– Aleksei Matiushkin
Nov 23 at 7:15
@AlekseiMatiushkin Yeah, it should be like that :) good one.
– ray
Nov 23 at 7:18
Seriously? Crappy Rails helper to parse a date from a string?
– Aleksei Matiushkin
Nov 22 at 12:29
Seriously? Crappy Rails helper to parse a date from a string?
– Aleksei Matiushkin
Nov 22 at 12:29
@AlekseiMatiushkin Please suggest then better one.
– ray
Nov 23 at 7:13
@AlekseiMatiushkin Please suggest then better one.
– ray
Nov 23 at 7:13
Looks like I did in the answer that was accepted.
– Aleksei Matiushkin
Nov 23 at 7:15
Looks like I did in the answer that was accepted.
– Aleksei Matiushkin
Nov 23 at 7:15
@AlekseiMatiushkin Yeah, it should be like that :) good one.
– ray
Nov 23 at 7:18
@AlekseiMatiushkin Yeah, it should be like that :) good one.
– ray
Nov 23 at 7:18
add a comment |
up vote
0
down vote
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
I got an ArgumentError: comparison of DateTime with 0 failed
– pfc
Nov 22 at 13:47
DateTime.new
in the last line looks a bit redundant :)
– Aleksei Matiushkin
Nov 22 at 13:54
@AlekseiMatiushkin You are right ;) I corrected it
– knut
Nov 22 at 22:13
add a comment |
up vote
0
down vote
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
I got an ArgumentError: comparison of DateTime with 0 failed
– pfc
Nov 22 at 13:47
DateTime.new
in the last line looks a bit redundant :)
– Aleksei Matiushkin
Nov 22 at 13:54
@AlekseiMatiushkin You are right ;) I corrected it
– knut
Nov 22 at 22:13
add a comment |
up vote
0
down vote
up vote
0
down vote
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
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
edited Nov 22 at 22:11
answered Nov 22 at 12:40
knut
22k46297
22k46297
I got an ArgumentError: comparison of DateTime with 0 failed
– pfc
Nov 22 at 13:47
DateTime.new
in the last line looks a bit redundant :)
– Aleksei Matiushkin
Nov 22 at 13:54
@AlekseiMatiushkin You are right ;) I corrected it
– knut
Nov 22 at 22:13
add a comment |
I got an ArgumentError: comparison of DateTime with 0 failed
– pfc
Nov 22 at 13:47
DateTime.new
in the last line looks a bit redundant :)
– Aleksei Matiushkin
Nov 22 at 13:54
@AlekseiMatiushkin You are right ;) I corrected it
– knut
Nov 22 at 22:13
I got an ArgumentError: comparison of DateTime with 0 failed
– pfc
Nov 22 at 13:47
I got an ArgumentError: comparison of DateTime with 0 failed
– pfc
Nov 22 at 13:47
DateTime.new
in the last line looks a bit redundant :)– Aleksei Matiushkin
Nov 22 at 13:54
DateTime.new
in the last line looks a bit redundant :)– Aleksei Matiushkin
Nov 22 at 13:54
@AlekseiMatiushkin You are right ;) I corrected it
– knut
Nov 22 at 22:13
@AlekseiMatiushkin You are right ;) I corrected it
– knut
Nov 22 at 22:13
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%2f53430270%2fwhy-datetime-new-returns-year-only%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