Created
July 11, 2017 11:13
-
-
Save hcn1519/4dbb6d63c3cfeb35629c1a14964503c0 to your computer and use it in GitHub Desktop.
This file contains 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
// | |
// main.swift | |
// DateTime | |
// | |
// Created by woowabrothers on 2017. 7. 11.. | |
// Copyright © 2017년 woowabrothers. All rights reserved. | |
// | |
import Foundation | |
typealias CurrentTime = (year: Int, month: Int, day: Int, hour: Int, minute: Int, second: Int) | |
class MyCalendar{ | |
let date = Date() | |
var calendar = Calendar.current | |
// 현재 시간 가져오기 | |
func getTime() -> CurrentTime { | |
let year = calendar.component(.year, from: date) | |
let month = calendar.component(.month, from: date) | |
let day = calendar.component(.day, from: date) | |
let hour = calendar.component(.hour, from: date) | |
let minutes = calendar.component(.minute, from: date) | |
let seconds = calendar.component(.second, from: date) | |
let result: CurrentTime = (year, month, day, hour, minutes, seconds) | |
return result | |
} | |
// Int 형식을 Date로 리턴 | |
func makeDateByInt(year: Int, month: Int, day: Int) -> Date { | |
var localCalendar = Calendar.current | |
var component = DateComponents() | |
component.year = year | |
component.month = month | |
component.day = day | |
localCalendar.timeZone = TimeZone(abbreviation: "GMT+0.00")! | |
let resultDate = localCalendar.date(from: component) | |
return resultDate! | |
} | |
// String 형식을 Date로 리턴 | |
func makeDateByString(year: String, month: String, day: String) -> Date { | |
// 날짜 설정 | |
var component = DateComponents() | |
component.year = Int(year) | |
component.month = Int(month) | |
component.day = Int(day) | |
// timeZone 설정 | |
var localCalendar = Calendar(identifier: .gregorian) | |
localCalendar.timeZone = TimeZone(abbreviation: "GMT+0.00")! | |
let resultDate = localCalendar.date(from: component) | |
return resultDate! | |
} | |
// 한국식 날짜 리턴 | |
func dateToKoreanDate(inputDate: Date) -> String { | |
let dateFormatter = DateFormatter() | |
dateFormatter.dateFormat = "yyyy년 MM월 dd일" | |
let resultString = dateFormatter.string(from: inputDate) | |
return resultString | |
} | |
// 날짜를 통해서 1일의 요일 구하기 | |
func getDateOfMonth(year: String, month: String) -> String { | |
let formatter = DateFormatter() | |
formatter.dateFormat = "yyyy-MM-dd" | |
guard let targetDate = formatter.date(from: year + "-" + month + "-" + "01") else { return "" } | |
var calendar = Calendar(identifier: .gregorian) | |
calendar.timeZone = TimeZone(secondsFromGMT: -9 * 60 * 60)! | |
let weekDay = calendar.component(.weekday, from: targetDate) | |
switch weekDay { | |
case 1: | |
return "월요일" | |
case 2: | |
return "화요일" | |
case 3: | |
return "수요일" | |
case 4: | |
return "목요일" | |
case 5: | |
return "금요일" | |
case 6: | |
return "토요일" | |
case 7: | |
return "일요일" | |
default: | |
fatalError() | |
} | |
} | |
func makeCalendarArray(year: String, month: String) -> [[Int]] { | |
var calendar = Calendar(identifier: .gregorian) | |
calendar.timeZone = TimeZone(abbreviation: "GMT+0.00")! | |
let formatter = DateFormatter() | |
formatter.dateFormat = "yyyy-MM" | |
guard var interval = calendar.dateInterval(of: .month, for: Date()) else { return [] } | |
interval.end.addTimeInterval(-1) | |
var resultArr: [[Int]] = [] | |
let lastDay = calendar.component(.day, from: interval.end) // 31 | |
var weekDay = calendar.component(.weekday, from: interval.start) | |
var day = 1 | |
while day <= lastDay { | |
var weekArr: [Int] = [] | |
while day <= lastDay { | |
if weekDay <= 7 { | |
weekArr.append(day) | |
day += 1 | |
weekDay += 1 | |
} else { | |
weekDay = 1 | |
resultArr.append(weekArr) | |
break | |
} | |
} | |
if day >= lastDay { | |
resultArr.append(weekArr) | |
} | |
} | |
return resultArr | |
} | |
} | |
let calendar = MyCalendar() | |
print(calendar.getTime()) | |
print(calendar.makeDateByInt(year: 1998, month: 12, day: 25)) | |
print(calendar.makeDateByString(year: "2016", month: "4", day: "16")) | |
print(calendar.dateToKoreanDate(inputDate: calendar.makeDateByString(year: "1998", month: "12", day: "25"))) | |
print(calendar.getDateOfMonth(year: "2010", month: "01")) | |
print(calendar.makeCalendarArray(year: "2010", month: "04")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment