Learn Courses My Dashboard

DateComponent to String Help

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


let dayDiffs = Calendar.current.dateComponents([.day], from: date1, to: date2date)


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



dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ssZ"

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.

Just FYI.

my bad, I knew something about that then couldn’t find it. thank you for your help!

The lines I have in already, work. (But I’m sure are not optimal, I just need it to work, then I can go back and make it cleaner hopefully)

Interestingly, with or without the single quotes, it doesn’t seem to make a difference on that line.

this is the line that doesn’t do what I want: (which is take dayDiffs and make it a string I can concatenate and drop first and last from.

var dayDiffsFormatted = Self.formatter.string(dayDiffs)

I get a fatal error if it has the single quotes because dateFormatter.date(from:date2)! returns nil and crashes on the force unwrap.

At any rate, for the printing, you can just do:

String(describing: dayDiffs)

to get the output:

"day: 22 isLeapMonth: false"

If you just want the number of days, you can do this:


which returns:

1 Like

wow. so simple. I’ve been fighting that for a day and a half. Every site made it 100x harder than I thought it would be.

thank you much!