LoginSignup
4
5

More than 5 years have passed since last update.

実行した日が誕生日だったらHappy Birthdayとprintするだけのスクリプト

Last updated at Posted at 2016-01-25

知り合いが誕生日だったので書いてみたら意外とめんどくさかった。
日付処理だからしょうがないか〜と思いつつすごく簡単な方法があったりして。。。

let name = "俺様"

let birthdayComponents = NSDateComponents()
birthdayComponents.year = 1982
birthdayComponents.month = 1
birthdayComponents.day = 18

if let calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian) {
    if let birthday = calendar.dateFromComponents(birthdayComponents) {
        let now = NSDate()
        let elapsed = calendar.components(([.Year, .Month, .Day]), fromDate: birthday, toDate: now, options: [])

        let year = elapsed.year
        let month = elapsed.month
        let day = elapsed.day

        switch (month, day) {
        case (0, 0):
            print("A Happy \(year)th birthday to \(name)🎉")
        default:
            print("A very mery unbirthday to you!")
        }
    }
}
4
5
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
5