Ruby/Rails date conversion using strptime. MM/DD/YY [duplicate] - ruby-on-rails

This question already has answers here:
Rails formatting date
(4 answers)
Closed 5 years ago.
I'm parsing PDF which returns date as string in format "04/27/17". How can I convert these strings to format for storing as date in rails?
Thanks!

Date::strptime can initialize a date object from a wacky US-based Y2K-unaware date format. Then if you have an active record class called MyObject with a date field called sweet_date, it would go like this:
date = Date::strptime("04/27/17", "%m/%d/%y") # returns Thu, 27 Apr 2017
object = MyObject.first
object.sweet_date = date
object.save # true

Related

Date format always return nil when using 12hr date format [duplicate]

This question already has answers here:
iOS NSDate Comparison works differently when the 24-Hour Time in date settings toggles between ON and OFF?
(2 answers)
DateFormatter doesn't return date for "HH:mm:ss"
(1 answer)
What is the best way to deal with the NSDateFormatter locale "feature"?
(4 answers)
Closed 4 years ago.
I'm trying to convert a string to a date, using the following code:
let dateFormmater = DateFormatter();
dateFormmater.timeZone = TimeZone(abbreviation: "UTC");
dateFormmater.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z";
let date = dateFormmater.date(from: "2018-06-13T15:33:47.796Z");
When the iPhone is set to 24hr mode, this works perfectly, and a new date object is created, but when the iPhone is set to 12hr mode, the date formatter returns nil.
Also, trying to get a string from date using the same date format while the iPhone is set to 24hr mode, results in a normal date, but using this in 12hr mode, results in the hours being presented both in 12hr and 24hr format,
e.g: 2018-07-05T208:17:22.019 PMZ.
note that the hour is 208 PM...
Has anyone encountered this before, and has a solution for this?
Cheers,
P.s this problem replicate able 1:1 in objective-c, using the corresponding classes.

How to convert sql timestamp to date in swift? [duplicate]

This question already has answers here:
Convert milliseconds to NSDate
(3 answers)
Closed 6 years ago.
I get timestamp results from database SQL query like 1465536311 or 1465540078. How to convert such number to current date and time in swift?
From NSDate Class Reference :
let date = NSDate(timeIntervalSince1970: 1465536311)

How to convert NSString into NSDate [duplicate]

This question already has answers here:
NSDate is 5 hours off
(4 answers)
Closed 7 years ago.
I want to convert my label.text into NSDate My formatter is MM/dd/yyyy. So I convert it like this.
mydate1=[dateFormatter dateFromString:depdate1txt.text];
My label value is (lldb) po depdate1txt.text
12/13/2015
But when I check the mydate1 it shows as (lldb) po mydate1
2015-12-12 18:30:00 +0000
Why date has changed into 12th december in 2015?
Please help me.
Thanks
When you print the date with po it's not formatting it - that's the base value. All dates are stored as a timestamp and only formatted when you explicitly format them.
The po command just takes a default formatter to show the date.
Oh, and the 'date change' is because of your timezone - the printed date is GMT +0.

Rails - how to throw explicit error when setting / persisting invalid date [duplicate]

This question already has answers here:
How do I validate a date in rails?
(10 answers)
Closed 7 years ago.
Okay so September has 30 days right.
This is what rails does when you set an invalid date like 31 September:
s = Student.first
s.date_of_birth = "2015-09-31"
s.save!
> true
s.date_of_birth
> Thu, 01 Oct 2015 00:00:00 UTC +00:00
It sets the date to 1st of October.
What I want is something like this
s.save!
(0.4ms) BEGIN
(0.5ms) ROLLBACK
ActiveRecord::RecordInvalid: Validation failed: Date is not a valid date
Is there no way to force Rails to explicitly error out when setting such a crap date? The standard rails date_select does not dynamically change the number of days in the days dropdown when you change to a different month. So you can select 31 September with the dropdowns, and the date will be saved as 1st of October. But I want to display an error message.
I don't want people to accidentally put the wrong dates into my system.
I think one of the solutions would be to change the datatype of date_of_birth from datetime to date. This way Rails would validate the date and would not allow the situation, you've shown.
It is also a good practice to store dates in date type, because it leads to significant decrease in potential issues, when working with those dates.
Try adding this to your model:
def date_of_birth=(d)
date = Date.strptime(d.to_s)
send(:write_attribute, :date_of_birth, date)
end
Now if you try setting an invalid date, it should raise an exception:
s.date_of_birth = "2015-09-31" # raises an exception
However, I don't think that doing this is a good idea. It's probably a better idea to do as Andrey Deineko said and store it in a date type.

Ruby: How modify Timezone without change time [duplicate]

This question already has answers here:
Ruby / Rails - Change the timezone of a Time, without changing the value
(14 answers)
Closed 7 years ago.
Ruby/Rails How to do this.
only modify the timezone of Time
2015-01-01 00:00:00 +0900 => 2015-01-01 00:00:00 +0000
take this date in variable say currentDateTime:
then use following:
currentDateTime.change(:offset => "+0000")

Resources