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)
Related
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.
This question already has answers here:
Comparing NSDates without time component
(17 answers)
Closed 5 years ago.
I've got a Date object that holds year/month/day and hour/minute/second data. I need to drop the hour/minute/second part as it's making comparing days problematic.
The issue is that the only obvious way I see of doing it is turning the day into a String and then using a DateFormatter with yyyy/mm/dd format to turn it BACK into a Date. This seems like a waste, is it really the only way?
Thanks a lot.
Never convert dates to String and back to Date.
You are looking for the startOfDay function of Calendar:
let date = Date()
let startOfDay = Calendar.current.startOfDay(for: date)
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
This question already has answers here:
What types of dates are these?
(1 answer)
In what format is this date string?
(2 answers)
Closed 9 years ago.
How can i parse the string "2013-06-13T05:47:03.737Z" to an NSDate instance using NSDateFormatter? I've tried setting the dateFormat property of my dateFormatter to be:
"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffZ", but i'm getting null. I hope U guys can help
Try this
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Convert NSDate to NSString
UPDATE: WHERE IS THE DIRECT ANSWER FROM THE DUPLICATE QUESTION
Please provide me the link for this answer.
How to convert NSDate to NSString like this
2012-10-17T10:13:15.343+02:00
So something along the lines of this: "yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"
For more information: Date Formatting Guide-Dates