UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Creating a variable for the current year... Error "Cannot infer contextual base in reference to member 'dateTime'"

Forums > Swift

I love the "Working with Dates" article that Paul wrote in 2021. Super helpful and infomative!

However I'm getting a surprising error when plugging the following into a Playground environment:

var currentYear = (Date.now, format: .dateTime.year())

Error: Cannot infer contextual base in reference to member 'dateTime'

What have I done wrong?

2      

In the example from Paul, he is using Text which is a View and needs a string. You cannot set and use the date format in that way, as it does not create a string, rather it needs a string input.

Try this

let formatter = DateFormatter()
formatter.setLocalizedDateFormatFromTemplate("YYYY")
// or   formatter.setLocalizedDateFormatFromTemplate("yyyy")
var currentYear = formatter.string(from: Date.now)

or if you want the year as a value.

var currentYear2 =  Calendar.current.component(.year, from: Date.now)

3      

I presume you got

(Date.now, format: .dateTime.year())

by way of Paul's example:

Text(Date.now, format: .dateTime.day().month().year())

But you'll notice that in Paul's example, he is doing this as part of a SwiftUI Text element.

You, on the other hand, are trying to format a Date as a String and assign it to a variable.

Those are two different things.

If you scroll down a little farther in Paul's article, you'll see another example that gives a hint what you need to do:

Text(Date.now.formatted(date: .long, time: .shortened))

Again, this is inside a Text but with a slight modification you can use the inner part to do what you want:

var currentYear = Date.now.formatted(.dateTime.year())

2      

formatter.setLocalizedDateFormatFromTemplate("YYYY")
// or   formatter.setLocalizedDateFormatFromTemplate("yyyy")

You should almost always use yyyy. The two will give the same result most of the time but can differ around the end of one year and beginning of the next.

yyyy gives you the calendar year.

YYYY gives you the year in "week of year" calendars.

3      

@twostraws  Site AdminHWS+

A small addition: both "yyyy" and "YYYY" can sometimes be wrong, so using just "y" is preferable. See here, from someone experienced with all the murky corners of dates and times: https://twitter.com/davedelong/status/1344388020661673985

3      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.