How to create a Date object in Swift? A detailed guide for beginners
Hello everyone! I just started learning Swift and am writing my first iOS app. I need to work with dates, but I can't figure out how to properly create a date object.
I read the Apple documentation, but it's all a bit confusing. In Objective-C I saw `NSDate`, and in Swift, as I understand, it's simply `Date`. But when I write:
```swift
let myDate = Date()
```
I get the current date and time. But what if I need to create a specific date, for example, December 25, 2023? I tried different options, but the compiler gives errors.
Here's what I tried:
```swift
let christmas = Date(2023, 12, 25) // Error!
let christmas2 = Date(year: 2023, month: 12, day: 25) // Also error
```
I also heard about `DateFormatter` and `Calendar`, but I don't understand how to use them together.
No answers yet.
Log in to leave an answer