Date string to Swift Date [duplicate] - swift

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 1 year ago.
I've tried using a DateFormatter() to format a string (from C# DateTime) into a Swift Date(). However, I keep getting nil.
Here is the code I've been playing around with:
let dateAndTimeString = "2021-08-24T10:16:06.647" //Copied from a C# API
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ" //Also trying without this dateFormat returns nil
let date = dateFormatter.date(from: dateAndTimeString) //Returns nil
If anyone could help point me in the right direction, that would be great!

The dateFormat you used is not matched with the given date string. The correct dateFormat should be below
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS"

Related

Swift 5 - Dateformatter not working as expected [duplicate]

This question already has answers here:
Swift - Get local date and time
(11 answers)
Closed 1 year ago.
I was playing around with date formatter in swift, but the AM/PM thing is not working in my code.
import Foundation
let dtstr = "Tuesday, July 28, 2020 4:15:45 PM"
let formatter = DateFormatter()
formatter.dateFormat = "eeee, MMMM d, yyyy h:m:s a"
let date = formatter.date(from: dtstr)
print(date)
the output is this: Optional(2020-07-28 08:15:45 +0000). However, it should be 16:15:45 instead of this. Any idea why?
Thanks!
Date has no information about time zone, and default string representation is using a greenwich one. You can see it +0000 part in your string.
You can get description for your own time zone like this:
date.description(with: .current)

Correct date format for timestamp when parsing with DateFormatter [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.
I have a timestamp string that takes the form:
2018-03-06T06:28:39.887Z
and need to format it into something more human readable, I have tried the below date format however the date doesn't parse
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "y-MM-ddTk:mm:ss.SSSZ"
let date = dateFormatter.date(from: "2018-03-06T06:28:39.887Z")
What am I missing with my date format?
Solution for Swift 3/4:
let dateFormatterGet = DateFormatter()
dateFormatterGet.dateFormat = "yyyy-MM-dd HH:mm:ss"
let dateFormatterPrint = DateFormatter()
dateFormatterPrint.dateFormat = "MMM dd,yyyy"
if let date = dateFormatterGet.date(from: "2016-02-29 12:24:26"){
print(dateFormatterPrint.string(from: date))
}
else {
print("There was an error decoding the string")
}

How can I work write with dates in Swift 4? [duplicate]

This question already has answers here:
Calculate time interval to 0.00 of the next day according to GMT in swift or objective-c?
(2 answers)
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.
I have a task for working with dates. The meaning of a task is to create a date in ISO format. I can do this:
var date = Date()
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZZ"
formatter.timeZone = TimeZone.current
formatter.locale = Locale(identifier: "ru_RU")
Well, the result of function is 2018-02-25T15:51:51+03:00
How can I output the beginning of a next day in this format?
I tried to deal with dataComponents but I failed.
I am a beginner in Swift 4 development and I need your help, please.

What is this timeformat? [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 5 years ago.
I want to use DateFormatter
but I don't know this time format
in Swift
let date = "2017-04-22T15:42:35.000Z"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd........"
print(dateFormatter.date(from: date))
2017-04-22T15:42:35.000Z
"yyyy-MM-dd...."
let date = "2017-04-22T15:42:35.000Z"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZZ"
print(dateFormatter.date(from: date))
This must be what you are looking for.

swift stored date turns different when formatted to string [duplicate]

This question already has answers here:
NSDate Format outputting wrong date
(5 answers)
Closed 7 years ago.
I stored a date in core data (as a date), and with the println it shows correctly its value: april 21 (is the var dateX below), but when right after the println i format it to string with the following code, the label linked to it shows april 22 (which is today, so i wonder tomorrow will show 23 etc.), where is the problem? anyone?
thank you
if dateX != nil{
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MMM dd, yyyy"
dateFormatter.timeZone = NSTimeZone.defaultTimeZone()
var dateXstring = dateFormatter.stringFromDate(dateX as NSDate)
startLabel.text = "Profile created on \(dateXstring)"
}
println dateX and dateXstring:
my time zone is Rome (Italy)
You likely have a timezone issue. Where are you located? DefaultTimeZone could be GMT/ZULU time which is -5 hrs from the east coast.
A good way to check is to use the timeIntervalSince1970 function (i think thats what it is called). If the stored date and retrieved date have the same value its the same date and you have a display problem.
timeIntervalSince1970 returns a NSTimeInterval which is really a Double