What is this timeformat? [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 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.

Related

Date string to Swift Date [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'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"

How to calculate Day(s) and Hour(s) remaining in swift between two dates? [duplicate]

This question already has answers here:
Getting the difference between two Dates (months/days/hours/minutes/seconds) in Swift
(20 answers)
Closed 3 years ago.
I have output 2019-11-23 and today's date 2019-11-4. How do I calculate the number of days remaining for these two dates? the output should be
19d5h
Try using DateFormatter and DateComponents to get this working.
Get the Date instances from String using DateFormatter and then get day and hour components using DateComponents, i.e.
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
if let d1 = dateFormatter.date(from: "2019-11-23"), let d2 = dateFormatter.date(from: "2019-11-4") {
let components = Calendar.current.dateComponents([.day, .hour], from: d2, to: d1)
print(components.day, components.hour)
}

How to get the Seconds from Date - Swift 4 [duplicate]

This question already has answers here:
Find difference in seconds between NSDates as integer using Swift
(7 answers)
Closed 4 years ago.
I am trying to get the number of SECONDS between two Dates.
for an example having a date like this:
let savedDate = Jun 25, 2018 at 12:48:09 AM -> (just printed as example date)
let currentDate = Date()
Can anyone help me understand how to find the number of second passed as a Double preferably?
Date#timeIntervalSince(_:) is probably what you're looking for.
Playground Example...
I modified the format so it would parse, but the basic concept works.
let formatter = DateFormatter()
formatter.dateFormat = "MMM dd, yyyy hh:mm:ss a"
let date = formatter.date(from: "Jun 25, 2018 12:48:09 AM")
if let date = date {
Date().timeIntervalSince(date)
}
Will output
29793.5867500305

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.