Convert current date into yyyy-MM-ddTHH:mm:ss.fffffffzzz format [duplicate] - ios

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.
Code:
let format1 = DateFormatter()
format1.dateFormat = "yyyy-MM-ddTHH:mm:ss.fffffffzzz"
format1.timeZone = TimeZone(abbreviation: "UTC")
print(format1.string(from:Date()))
I am trying to get date string in this format(2018-12-19T12:30:00.000
+11:00) but i got date(2018-12-12) only.what is wrong with mycode.any help will be appricated.thanks in advance

You are not using proper format, please try this will help you.
let format1 = DateFormatter()
format1.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS ZZZ"
format1.timeZone = TimeZone(abbreviation: "UTC")
format1.locale = Locale(identifier: "en_US_POSIX")
print(format1.string(from:Date()))
This will print following output:
2018-12-24T14:16:11.011 +0000

Related

How to convert a time string to 12 hour time in swift? [duplicate]

This question already has answers here:
Xcode Swift am/pm time to 24 hour format
(12 answers)
Closed 1 year ago.
import Foundation
let dateFormatter = DateFormatter()
var timy = "13:25:00"
dateFormatter.dateFormat = "HH:mm:ss "
print(dateFormatter.date(from: timy))
// this gives me output of differnet time i.e Optional(2000-01-01 07:55:00 +0000)
This is the correct format to account for AM/PM:
dateFormatter.dateFormat = "h:mm:ss a"

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
}

How do I convert this, "2021-07-05T22:26:51.159Z" to Date() with swift [duplicate]

This question already has an answer here:
Swift ISO8601 format to Date returning fatal error
(1 answer)
Closed 1 year ago.
I think this is an ISO8601 formatted timestamp.
2021-07-05T22:26:51.159Z
I'm trying to convert it with ISO8601DateFormatter() in swift 5.
Here's what I've tried:
let dateFormatter = ISO8601DateFormatter()
dateFormatter.formatOptions = .withFullDate
//ISO8601DateFormatter().formatOptions = .withFractionalSeconds
let d = "2021-07-05T22:26:51.159Z"
let date = dateFormatter.date(from: d)
The result:
date = 2021-07-05 00:00:00 UTC
The day is correct, the time is not. I've tried to set the .withFractionalSeconds option. Didn't help.
How should I convert this format?
You can use standard date formatter to achieve this:
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"
let date = dateFormatter.date(from: d)
print(date)

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))

How to convert milliseconds to date string in swift 3 [duplicate]

This question already has answers here:
NSDate timeIntervalSince1970 not working in Swift? [duplicate]
(3 answers)
Closed 6 years ago.
I am trying to convert milliseconds to date string in swift 3,i tried by setting date fomatter but i am not getting current date string.
var milliseconds=1477593000000
let date = NSDate(timeIntervalSince1970: TimeInterval(milliseconds))
let formatter = DateFormatter()
formatter.dateFormat = "dd-MM-yyyy HH:mm:ss"
formatter.locale = NSLocale(localeIdentifier: "en_US") as Locale!
print(formatter.string(from: date as Date))
output:
22-01-48793 01:30:00
Try this,
var date = Date(timeIntervalSince1970: (1477593000000 / 1000.0))
print("date - \(date)")
You will get output as date :
date - 2016-10-27 18:30:00 +0000
How about trying this -
let milisecond = 1479714427
let dateVar = Date.init(timeIntervalSinceNow: TimeInterval(milisecond)/1000)
var dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy hh:mm"
print(dateFormatter.string(from: dateVar))
Have a look at the documentation of NSDate:
convenience init(timeIntervalSince1970 secs: TimeInterval)
Returns an NSDate object initialized relative to the current date and time by a given number of seconds.
Just convert your milliseconds to seconds and you should get the correct date.

Resources