NSDateFormatter for datetime [duplicate] - ios

This question already has answers here:
How do I get an ISO 8601 date on iOS?
(12 answers)
Closed 7 years ago.
I have a string which is coming from an API that I need to convert into a NSDate but I'm uncertain which dateFormat to use for NSDateFormatter.
let openedAt = "2015-06-30T12:34:00.000-04:00" // coming from API
let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy-MM-dd..." // not sure what format to use here
if let date = formatter.dateFromString(openedAt) {
println(date)
} else {
println("NOPE!")
}

try
"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"
The Date Format Patterns guide suggests that "S" is the format specifier for fractions of seconds.

Related

What is the correct date format for this date-as-a-string? [duplicate]

This question already has answers here:
How to convert "2017-07-11T06:52:15.948Z" in to like "JUL, 7 2017" in swift
(1 answer)
How can I parse / create a date time stamp formatted with fractional seconds UTC timezone (ISO 8601, RFC 3339) in Swift?
(13 answers)
Closed 1 year ago.
The back end is providing me with this date-as-a-string: 2021-09-10T12:57:01.671Z
I need to convert that string to a Date using the iOS DateFormatter; and to do so I need to set the formatter's dateFormat property.
I have tried all sorts of combinations, with no luck. Specifically I am struggling with the .671Z part.
What is the correct date format to use?
You need "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" as date format
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
guard let date = dateFormatter.date(from: self) else {
// fallback if date is not in correct format
return nil
}

Convert from 2018-10-23T06:01:10.806Z to 10-May-2018 and a seperate string for time [duplicate]

This question already has answers here:
How can I parse / create a date time stamp formatted with fractional seconds UTC timezone (ISO 8601, RFC 3339) in Swift?
(13 answers)
Closed 4 years ago.
have to extract a separate date and time from the same string i.e)2018-10-23T06:01:10.806Z, the date must be in the format of 10-May-2018 and the time must be in 12 hours format i.e) 08:00 PM
Try this
func formatDateString(dateString: String) -> Date? {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss'Z'"
return dateFormatter.date(from: dateString)
}
You get the Date object, do whatever you can do whatever you wanted using that.

Date From Timestamp Becomes Wrong [duplicate]

This question already has answers here:
NSDate timeIntervalSince1970 not working in Swift? [duplicate]
(3 answers)
Closed 4 years ago.
I am trying to convert timestamp to Date. But it returns wrong date.
I have a time stamp
1524637838000.0
Which returns 25-04-2018 12:00 as per this online converter
But I get wrong date when convert using my code
let date = Date(timeIntervalSince1970:1524637838000.0)
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy hh:mm"
let myDate = dateFormatter.string(from: date)
print("Date is = ",myDate)
I get
07-11-50283 12:03
as result. Is there anything wrong with my code?
your timestamp 1524637838000.0 in milliseconds, it should be in seconds, so your date initialization should be:
let date = Date(timeIntervalSince1970:(1524637838000.0/1000))

iOS date convertor to return proper date [duplicate]

This question already has answers here:
NSDate timeIntervalSince1970 not working in Swift? [duplicate]
(3 answers)
Closed 5 years ago.
Having a problem with date conversion, I'm pulling a timestamp value from firebase database and try to change it into a Date string.
Below is a struct to convert the Timestamp value into a Date string.
struct DateConverter {
private let date : Double?
init(dateToConvert: Double){
self.date = dateToConvert
}
func convertToString() -> String {
let cDate = Date(timeIntervalSince1970: TimeInterval(date!))
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy/MM/dd hh:mm a"
let dateInString = dateFormatter.string(from: cDate)
return dateInString
}
}
But unfortunately whenever i try to revert the double value the output seem completely wrong. e.g.:
1488873726317 (From firebase DB)
After using the custom date converter to convert the Double value into Date string here's the final output.
49150/07/14 03:05
The year is messed up already, Am i missing something here ?
You need to divide maybe,
let date = Date(timeIntervalSince1970:addServerStamp/1000)

How to convert date which is having String type to NSDate? [duplicate]

This question already has an answer here:
How to convert String to NSDate?
(1 answer)
Closed 7 years ago.
I am developing an iOS app using Swift. I am accessing data from server and it is returning data in JSON format. In that I am having a field 'ExpiryDate' which is getting from that JSON and converted to string as follows:
var validityDate = offerElements[indexPath.row]["endDate"].stringValue
It is returning date of following format:
2015-08-24T00:00:00.000Z
I wanna convert it to MM/DD/YYYY, how can I do it ?
Your suggestions are most welcome. Thank you in advance.
This should work with minimal or no changes
func dateFromString(dateString: String) -> NSDate {
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
return dateFormatter.dateFromString(dateString)!
}

Resources