Last active
January 10, 2017 01:27
-
-
Save ha1f/e01fd3044a6132a6d894fd484b69be12 to your computer and use it in GitHub Desktop.
カレンダー周り。日本語アプリでも英語表示したい部分もあると思うので
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// CalendarUtil.swift | |
// Musubi | |
// | |
// Created by はるふ on 2016/10/26. | |
// Copyright © 2016年 はるふ. All rights reserved. | |
// | |
import Foundation | |
class CalendarUtil { | |
static let calendar = Calendar(identifier: .gregorian) | |
static func getComponents(_ components: Set<Calendar.Component>, from: Date) -> DateComponents { | |
return calendar.dateComponents(components, from: from) | |
} | |
// 1~7 -> 日〜土 | |
static func getWeekdaySymbol(_ weekday: Int) -> String { | |
let weekdayIndex = ((weekday-1) % 7) | |
let formatter = DateFormatter() | |
formatter.locale = Locale.current | |
return formatter.shortWeekdaySymbols[weekdayIndex] | |
} | |
// 1〜7 -> Sunday〜Saturday | |
static func getWeekdaySymbolLongEnglish(_ weekday: Int) -> String { | |
let weekdayIndex = ((weekday-1) % 7) | |
let formatter = DateFormatter() | |
formatter.locale = Locale(identifier: "en_US_POSIX") | |
return formatter.weekdaySymbols[weekdayIndex] | |
} | |
// 1〜12 -> January 〜 December | |
static func getMonthSymbolLongEnglish(_ month: Int) -> String { | |
let monthIndex = ((month-1) % 12) | |
let formatter = DateFormatter() | |
formatter.locale = Locale(identifier: "en_US_POSIX") | |
return formatter.monthSymbols[monthIndex] | |
} | |
// Date -> 12:34 | |
static func getTime24String(from date: Date) -> String { | |
let formatter = DateFormatter() | |
formatter.locale = Locale(identifier: "en_US_POSIX") | |
formatter.dateFormat = "HH:mm" | |
return formatter.string(from: date) | |
} | |
// Date -> 2015/9, 9/2015 | |
static func getYearMonthString(from date: Date, locale: Locale = Locale.current) -> String { | |
let formatter = DateFormatter() | |
formatter.dateFormat = DateFormatter.dateFormat(fromTemplate: "yM", options: 0, locale: locale)! | |
return formatter.string(from: date) | |
} | |
// Date -> 12月34日 | |
static func getDateString(from date: Date) -> String { | |
let formatter = DateFormatter() | |
formatter.dateStyle = .medium | |
formatter.timeStyle = .none | |
return formatter.string(from: date) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment