Created
November 8, 2016 21:31
-
-
Save sergii-frost/7738d0a546fef89c5a8c64e41ec27460 to your computer and use it in GitHub Desktop.
DateHelper playground
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
//: Playground - noun: a place where people can play | |
import UIKit | |
var str = "Hello, Date Helper playrgound!" | |
//Returns the first date of the current year. Should return the following timestamp for the year 2016: 1451606400 | |
func getFirstDateOfCurrentYear() -> NSNumber { | |
//Get current year | |
let currentDate = NSDate() | |
let calendar = NSCalendar.currentCalendar() | |
calendar.timeZone = NSTimeZone(abbreviation: "UTC")! | |
let components = calendar.components([.Day , .Month , .Year], fromDate: currentDate) | |
components.day = 1 | |
components.month = 1 | |
// Get NSDate given the above date components | |
let date = calendar.dateFromComponents(components) | |
let dateTimeInterval = (date?.timeIntervalSince1970)! | |
return Int(dateTimeInterval) | |
// return 1451606400 | |
} | |
//Returns the last date of the year 2040. Should return the following timestamp: 2240524800 | |
func getLastDateAvailable() -> NSNumber { | |
let calendar = NSCalendar.currentCalendar() | |
calendar.timeZone = NSTimeZone(abbreviation: "UTC")! | |
let c = NSDateComponents() | |
c.year = 2040 | |
c.month = 12 | |
c.day = 31 | |
// Get NSDate given the above date components | |
let date = calendar.dateFromComponents(c) | |
let dateTimeInterval = (date?.timeIntervalSince1970)! | |
return Int(dateTimeInterval) | |
// return 2240524800 | |
} | |
print("Timezone: \(NSTimeZone.systemTimeZone())") | |
print("Timezone: \(NSTimeZone.localTimeZone())") | |
print("Timezone: \(NSTimeZone(abbreviation: "UTC")!)") | |
print("getFirstDateOfCurrentYear") | |
print("->: \(1451606400)") | |
print("=>: \(getFirstDateOfCurrentYear())") | |
print("getLastDateAvailable") | |
print("->: \(2240524800)") | |
print("=>: \(getLastDateAvailable())") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment