I’m trying to get it to output the “day: XX isLeapMonth: false” as just a string so I can manipulate it down to just the “XX” part.
Thank you for any help! I’m obviously new to Swift, came from HTML/ASP/SQL a decade ago!
In ASP it was 100x easier to go from int to string to date etc. You just kinda did it and ASP took a good guess.
I’ve tried 100 different ways to get that output to just a string, but no dice… This is just in playground so I could focus just on this chunk of code.
import UIKit
import Foundation
let formatter: DateFormatter = {
let dateformatter = DateFormatter()
dateformatter.dateStyle = .medium
dateformatter.timeStyle = .short
dateformatter.locale = .current
return dateformatter
}()
let date1 = Date()
let date2 = "2021-12-08 10:00:00 +0000"
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "yyyy-MM-dd' 'HH:mm:ssZ"
let date2date = dateFormatter.date(from:date2)!
//print(date2)
let dayDiffs = Calendar.current.dateComponents([.day], from: date1, to: date2date)
print(dayDiffs)
OUTPUTS: "day: 22 isLeapMonth: false "
which I’d like in String, as when I try to manipulate it, I just get yelled at by Swift… thanks
When posting code to these forums, place three backticks ``` on the line before your code and three backticks ``` on the line after your code so that it will be formatted properly. You can also highlight an entire code block and click the </> button on the toolbar to wrap the block for you.
This makes it far easier to read and also makes it easier for other posters to copy/paste the code in order to test solutions and such.
You might also notice that the quote marks are messed up in your original post because of not putting a code fence around your code. Discourse “helpfully” changes double quotes " to unicode double quotes “ and Xcode doesn’t like those. It’s not a big deal to correct them in Xcode but it’s a bit more friction.