Skip to content

Instantly share code, notes, and snippets.

@DisappearPing
Last active March 16, 2016 04:09
Show Gist options
  • Save DisappearPing/02b15e5d6fdca955210a to your computer and use it in GitHub Desktop.
Save DisappearPing/02b15e5d6fdca955210a to your computer and use it in GitHub Desktop.
string -> define date -> define string -> string
import Foundation
// string -> define date -> define string -> string
extension NSDate {
var formattedString: String{
let formatter = NSDateFormatter()
// ex: 12.04 -> NSDate
formatter.dateFormat = "MM.dd"
formatter.timeZone = NSTimeZone(name: "Asia/Taipei")
return formatter.stringFromDate(self)
}
var pickerFormattedString: String{
let formatter = NSDateFormatter()
// ex: 1990.12.04 -> NSDate
formatter.dateFormat = "yyyy/MM/dd"
formatter.timeZone = NSTimeZone(name: "Asia/Taipei")
return formatter.stringFromDate(self)
}
func isGreaterThanDate(dateToCompare : NSDate) -> Bool
{
//Declare Variables
var isGreater = false
//Compare Values
if self.compare(dateToCompare) == NSComparisonResult.OrderedDescending
{
isGreater = true
}
//Return Result
return isGreater
}
func isLessThanDate(dateToCompare : NSDate) -> Bool
{
//Declare Variables
var isLess = false
//Compare Values
if self.compare(dateToCompare) == NSComparisonResult.OrderedAscending
{
isLess = true
}
//Return Result
return isLess
}
func isEqualDate(dateToCompare : NSDate) -> Bool
{
//Declare Variables
var isEqualTo = false
//Compare Values
if self.compare(dateToCompare) == NSComparisonResult.OrderedSame
{
isEqualTo = true
}
//Return Result
return isEqualTo
}
}
extension String {
var asNSDate:NSDate {
let styler = NSDateFormatter()
// ex: NSDate -> 1990-12-04 12:12
styler.dateFormat = "yyyy-MM-dd HH:mm"
styler.timeZone = NSTimeZone(name: "Asia/Taipei")
return styler.dateFromString(self) ?? NSDate()
}
var pickerAsNSDate:NSDate {
let styler = NSDateFormatter()
// ex: NSDate -> 1990-12-04 12:12:12 +0000
styler.dateFormat = "yyyy-MM-dd HH:mm:ss ZZZ"
styler.timeZone = NSTimeZone(name: "Asia/Taipei")
return styler.dateFromString(self) ?? NSDate()
}
var compareAsNSDate:NSDate {
let styler = NSDateFormatter()
// ex: NSDate -> 1990/12/04
styler.dateFormat = "yyyy/MM/dd"
styler.timeZone = NSTimeZone(name: "Asia/Taipei")
return styler.dateFromString(self) ?? NSDate()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment