Search code examples
iosswiftmacosfoundationnsdatecomponents

DateComponents doesn't seem to honor year


I'm attempting to convert a Date to a DateComponents, change a few properties, and get a new Date back. I've written the following code:

import Foundation
var components = Calendar.current.dateComponents(in: .current, from: Date())
components.day = 7
components.month = 3
components.year = 1900
DateFormatter.localizedString(from: components.date!, dateStyle: .long, timeStyle: .long)

I've tested this in the America/Denver time zone/locale on an iOS Simulator on iOS 15.0 as well as a Swift REPL on my Mac running the latest macOS Big Sur and Xcode 13.0, and in both places, I get approximately the following output at the time of this writing:

March 7, 2021 at 9:38:13 AM MST

Everything about this is as expected, except for the year. I had explicitly set the year to be 1900, but the year in the output is 2021. How can I make DateComponents honor the year when generating a date, or how can I do this same kind of thing manually so it'll actually work?


Solution

  • If you get all components from a date with dateComponents(in:from:), you have to set also yearForWeekOfYear accordingly

    components.yearForWeekOfYear = 1900
    

    Or specify only the date and time components including calendar and timeZone you really need for example

    var components = Calendar.current.dateComponents([.calendar, .timeZone, .year, .month, .day, .hour, .minute, .second], from: Date())
    components.day = 7
    components.month = 3
    components.year = 1900
    DateFormatter.localizedString(from: components.date!, dateStyle: .long, timeStyle: .long)