What is the date formater for this string? [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 2 years ago.
I am working in Swift language. I don't know the date format for this 2020-07-23T00:17:06.000Z.
I want to convert Date to String by following that format & vice versa.
How can I do? Thanks in advance...

it is iso8601
let string = "2020-07-23T00:17:06.000Z"
let dateFormatter = ISO8601DateFormatter()
dateFormatter.formatOptions = [.withInternetDateTime, .withFractionalSeconds]
let date = dateFormatter.date(from: string)

Related

DateFormatter and ISO8601DateFormatter - various time zones in result [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)
DateFormatter returns previous day
(1 answer)
Closed 6 months ago.
I have Swift 5 and such date string:
let dateString = "2019-11-09T08:14:09.361404Z"
and two approaches to convert it to date:
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSSSS'Z'"
let date = dateFormatter.date(from: dateString)
let dateFormatter = ISO8601DateFormatter()
dateFormatter.formatOptions = [.withFractionalSeconds, .withInternetDateTime]
let date = dateFormatter.date(from: dateString)
as result:
first case: 2019-11-09 05:14:09 +0000
second case: 2019-11-09 08:14:09 +0000
Dates differ by 3 hours (just like my timezone differs from GMT). Setting the timezone to my current both formatters doesn't affect the results.
Could you please explain difference and which result is correct?

Unable to convert string to date, string contains dot with 6 character [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 1 year ago.
I stuck to convert string to date, my date string is
"2021-03-25T06:35:36.372245"
Unable to convert it into date formate. I used the code but unable to convert, i got nil.
My code is
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-ddTHH:mm:ss.ZZZZZZ" ( also tried SSSSSS)
let dte = dateFormatter.date(from: dateTime)
but unable to parse it. Kindly help me out.
You forget '' arround T
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSSSS"
dateFormatter.timeZone = TimeZone(identifier: "UTC")
Do this
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS"
let dte = dateFormatter.date(from: dateTime)

Convert String into Date? [duplicate]

This question already has answers here:
How do I convert a date/time string to a DateTime object in Dart?
(8 answers)
Closed 2 years ago.
I am getting date in String and I want to convert it into given Date format. Can anyone please guide me how can I do it ?
String myDate = 'June-2020'
I am getting date like this and I want to convert it into this 2020-06-01 format. 01 is default for every month.
var convertedDate = '2020-06-01'
You can create custom date format using intl package
String dateString1 = 'June-2020';
String dateString2 = 'May-2021';
DateFormat dateFormate = DateFormat('MMMM-yyyy');
DateTime date1 = dateFormate.parse(dateString1);
DateTime date2 = dateFormate.parse(dateString2);
print(date1); //2020-06-01 00:00:00.000
print(date2); //2020-05-01 00:00:00.000

Swift String Date, "2015-06-18T17:02:02.614Z" to Local Date [duplicate]

This question already has answers here:
Convert date string swift
(3 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 2 years ago.
Here is my code to convert but I had a nil value. Any help is appreciated
func serverToLocal(date:String) -> Date? {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
dateFormatter.timeZone = TimeZone(abbreviation: "GTM")
let localDate = dateFormatter.date(from: date)
return localDate
}
And he is my call:
let res = self.serverToLocal(date: "2015-06-18T17:02:02.614Z")

Get wrong date value after converting unix time stamp [duplicate]

This question already has answers here:
How to Convert UNIX epoch time to Date and time in ios swift
(3 answers)
Closed 2 years ago.
I get a unix timestamp from server
1589275703283
when I tried to convert it from this website: https://www.epochconverter.com
I get the value of Tuesday, 12 May 2020
and I'm using this code to convert this timestamp to date that I can read
func timeStringFromUnixTime(timestamp: Int64) -> String {
let date = Date(timeIntervalSince1970: TimeInterval(timestamp))
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd, MMMM yyyy"
dateFormatter.timeZone = TimeZone(identifier: NSTimeZone.default.identifier)
let localDate = dateFormatter.string(from: date)
return localDate
}
but it passed me 22 February 52332, which is obviously wrong
Could anyone help me where the problem come from?
thanks in advance
The timestamp 1589275703283 is way too big for a "normal" UNIX timestamp. It's in milliseconds instead of seconds (which epochconverter.com is smart enough to detect; it's printing "Assuming that this timestamp is in milliseconds"). So you need to divide your timestamp by 1000:
let date = Date(timeIntervalSince1970: TimeInterval(timestamp) / 1000)