Swifterswift: Date.year not working

Created on 25 Oct 2017  路  9Comments  路  Source: SwifterSwift/SwifterSwift

[x] I've read, understood, and done my best to follow the Contributing guidelines before opening this issue.

What did you do?

I used Date.year extension to update year component of a date.

What did you expect to happen?

year component to be changed.

What happened instead?

Year didn't change.

SwifterSwift Environment

  • SwifterSwift version: 4.0.1
  • Xcode version: 9.0.1
  • macOS version running Xcode: High Sierra
  • Swift version: 4.0
  • Platform(s) running SwifterSwift: iOS

Demo Code

var date = Date()
print("Year before change: \(date.year)") // "Year before change: 2017"
date.year = 2010
print("Year after change: \(date.year)") // "Year after change: 2017", should be 2010
source good first issue

Most helpful comment

i'll look into this if that's ok

All 9 comments

@omaralbeik Can you try date.year = 1 and let me know what the output is? 馃

Nevermind, I thought maybe we were using the wrong method. Not sure on this one

i'll look into this if that's ok

I found this stack overflow thread about it.
apparently Calendar.current.date(bySetting: .year, value: newValue, of: self) is returning nil for some reason.

@kaphacius Absolutely!, contributions are always welcome and appreciated 馃槂

it returns nil if the year is in the past for some reason. i'll use a different way of constructing a Date

@SD10, @kaphacius I think we should add tests for dates at past and future for other components as well

299 fixes this

i'll add tests for past dates soon-ish :)

This is really strange, the apple docs for date(bySetting:value:of:) do not say anything about the case when it could return nil :(
Fun fact: this StackOverflow thread that @omaralbeik found has a link to this related issue on other GitHub repo with some alternatives to overcome this too.

Does this bug affect any other Date extensions that have a similar implementation?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

martinstoyanov picture martinstoyanov  路  5Comments

omaralbeik picture omaralbeik  路  3Comments

omaralbeik picture omaralbeik  路  3Comments

pawurb picture pawurb  路  3Comments

omaralbeik picture omaralbeik  路  3Comments