Date, Calendar, Component in swift

{% codeblock lang:swift %} // Playground - noun: a place where people can play

import UIKit
import Foundation

// Setup the calendar object let calendar = NSCalendar.currentCalendar()

// Set up date object let date = NSDate()

// Create an NSDate for the first and last day of the month //let components = calendar.components(NSCalendarUnit.CalendarUnitYear | // NSCalendarUnit.CalendarUnitMonth | // NSCalendarUnit.WeekdayCalendarUnit | // NSCalendarUnit.WeekCalendarUnit | // NSCalendarUnit.CalendarUnitDay, // fromDate: date)

// Create an NSDate for the first and last day of the month let components = NSCalendar.currentCalendar().components(NSCalendarUnit.CalendarUnitMonth | NSCalendarUnit.CalendarUnitDay | NSCalendarUnit.CalendarCalendarUnit, fromDate: date)

components.month
components.day

// Getting the First and Last date of the month components.day = 1
let firstDateOfMonth: NSDate = calendar.dateFromComponents(components)!

components.month += 1
components.day = 0
let lastDateOfMonth: NSDate = calendar.dateFromComponents(components)!

var unitFlags = NSCalendarUnit.WeekOfMonthCalendarUnit |
NSCalendarUnit.WeekdayCalendarUnit | NSCalendarUnit.CalendarUnitDay

let firstDateComponents = calendar.components(unitFlags, fromDate: firstDateOfMonth)
let lastDateComponents = calendar.components(unitFlags, fromDate: lastDateOfMonth)

// Sun = 1, Sat = 7 let firstWeek = firstDateComponents.weekOfMonth
let lastWeek = lastDateComponents.weekOfMonth

let numOfDatesToPrepend = firstDateComponents.weekday - 1
let numOfDatesToAppend = 7 - lastDateComponents.weekday + (6 - lastDateComponents.weekOfMonth) * 7

let startDate: NSDate = calendar.dateByAddingUnit(NSCalendarUnit.CalendarUnitDay, value: -numOfDatesToPrepend, toDate: firstDateOfMonth, options: nil)!
let endDate: NSDate = calendar.dateByAddingUnit(NSCalendarUnit.CalendarUnitDay, value: numOfDatesToAppend, toDate: lastDateOfMonth, options: nil)!

Array(map(0..<42) {
calendar.dateByAddingUnit(NSCalendarUnit.CalendarUnitDay, value: $0, toDate: startDate, options: nil)! })

"(components.year)"

var dateString = "2014-10-3" // change to your date format
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "YYYY-MM-dd"

var someDate = dateFormatter.dateFromString(dateString)
println(someDate)

{% endcodeblock %}

比较两个日期的天数间隔:

{% codeblock lang:swift %} // Playground - noun: a place where people can play

import Foundation

let date = NSDate()
let startDate = NSDate()
let endDate = NSDate(timeIntervalSinceNow: 246060)

func diffDays(startDate: NSDate, endDate: NSDate) -> Int {
let calendar = NSCalendar.currentCalendar() var unitFlags = NSCalendarUnit.CalendarUnitDay | NSCalendarUnit.CalendarUnitMonth

let startComponents = NSCalendar.currentCalendar().components(unitFlags, fromDate: startDate)

let endComponents = NSCalendar.currentCalendar().components(unitFlags, fromDate: endDate)

let startBeginningDate = calendar.dateFromComponents(startComponents)!

let endBeginningDate = calendar.dateFromComponents(endComponents)!

// 注意。 components:fromDate:toDate:options:只适合用来获取相差的天,月,年单个数值比较。不适合处理闰年,跨年等场景
// let diffComponents = NSCalendar.currentCalendar().components(unitFlags, fromDate: startBeginningDate, toDate: endBeginningDate, options: nil)

// return diffComponents.day

let diffDays = Int(dateCreatedDate.timeIntervalSinceDate(dateNowDate) / (246060))

return diffDays }

diffDays(startDate, endDate)

func getWeek(dateCreated: NSDate) -> String {
let calendar = NSCalendar.currentCalendar() var unitFlags = NSCalendarUnit.WeekCalendarUnit | NSCalendarUnit.WeekdayCalendarUnit

// Sun = 1, Sat = 7

let weekComponents = calendar.components(unitFlags, fromDate: date) let weekKey = weekComponents.weekday

var weekText = "" switch weekKey { case 1: weekText = "Sun" case 2: weekText = "Mon" case 3: weekText = "Tue" case 4: weekText = "Wed" case 5: weekText = "Thu" case 6: weekText = "Fri" case 7: weekText = "Sat" default: weekText = "" } return weekText }

getWeek(date)

{% endcodeblock %}

Neil Wang

产品经理。喜欢摄影,滑板,电影。微信公众号:NeilOnly