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

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.

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)

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

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