-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDateUtils.swift
61 lines (48 loc) · 2.11 KB
/
DateUtils.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//
// DateUtils.swift
// Gank
//
// Created by 魏星 on 16/7/17.
// Copyright © 2016年 wx. All rights reserved.
//
import Foundation
public class DateUtils{
static let DATE_FORMATTER = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
public static func stringToNSDate(dateString: String,formatter: String = DATE_FORMATTER) -> NSDate {
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = formatter
return dateFormatter.dateFromString( dateString )!
}
public static func nsDateToString(date: NSDate,formatter: String = "yyyy-MM-dd HH:mm:ss" ) -> String {
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = formatter
return dateFormatter.stringFromDate(date)
}
public static func areDatesSameDay(dateOne:NSDate,dateTwo:NSDate) -> Bool {
let calender = NSCalendar.currentCalendar()
let flags: NSCalendarUnit = [.Day, .Month, .Year]
let compOne: NSDateComponents = calender.components(flags, fromDate: dateOne)
let compTwo: NSDateComponents = calender.components(flags, fromDate: dateTwo);
return (compOne.day == compTwo.day && compOne.month == compTwo.month && compOne.year == compTwo.year);
}
public static func getYMDTupleArray() ->[(String,String,String)]{
var date = NSDate()
var ymdTuples:[(String,String,String)] = []
for _ in 0...4{
date = getDateYesterday(date)
ymdTuples += [getYMDTypleFromDate(date)]
}
return ymdTuples
}
private static func getDateYesterday(date: NSDate) -> NSDate{
let calender = NSCalendar.currentCalendar()
return calender.dateByAddingUnit(.Day, value: -1, toDate: date, options: .MatchStrictly)!
}
private static func getYMDTypleFromDate(date: NSDate) -> (String,String,String){
let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy/MM/dd"
let datesStr = formatter.stringFromDate(date)
let dates = datesStr.componentsSeparatedByString("/")
return (dates[0],dates[1],dates[2])
}
}