LoginSignup
6
4

More than 5 years have passed since last update.

OS Xのメニューから呼び出せるカレンダーを作ってみる

Last updated at Posted at 2017-08-16

夏休みの工作というか

開発環境

  • XCode8.3.3
  • Swift3
  • OS X 10.12.6

スクリーンショット 2017-08-16 01.12.11.png

元ネタはこちらです。
OS X Tutorial: Menus and Popovers in Menu Bar Apps

githubはこちら
MenuCalendar

ちょっと今日が何周目かと確認したい時に、メニューで見れるカレンダーが欲しいと思っていたので、作ってみました。

Swiftで日付をいじるやり方をいつも忘れるので今回のやり方をメモがわりに

        let calendar = Calendar.current
        var comps = calendar.dateComponents([.year, .month, .day, .hour, .minute, .second], from: Date())

        day = comps.day
        month = comps.month
        year = comps.year

        //月初
        comps.day = 1
        comps.hour = 0
        comps.minute = 0
        comps.second = 0
        let firstdate = calendar.date(from: comps)
        comps = calendar.dateComponents([.year, .month, .weekday], from: firstdate!)
        firstWeekDay = comps.weekday! - 1

        let mon = comps.month! > 10 ? String(describing: comps.month!) : "0" + String(describing: comps.month!)
        thisMonth = String(describing: comps.year!) + mon

        let range = calendar.range(of: .day, in: .month, for: date)!
        月末
        endDay = range.upperBound - 1
//月単位の加算
var comp = DateComponents()
        comp.month = m
        let calendar = Calendar.current
        date = calendar.date(byAdding: comp, to: date)!
6
4
2

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
6
4