Last active
March 13, 2018 10:45
-
-
Save jaderfeijo/5f02e15c28c3dee67ec25a032da76983 to your computer and use it in GitHub Desktop.
String+Dates.swift
This file contains hidden or 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
// | |
// String+Dates.swift | |
// Created by Jader Feijo on 14/10/2016. | |
// | |
import Foundation | |
extension String { | |
func asDate(withFormat format: String) -> Date? { | |
let formatter = DateFormatter() | |
formatter.dateFormat = format | |
return formatter.date(from: self) | |
} | |
} |
This file contains hidden or 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
// | |
// StringDatesTests.swift | |
// Created by Jader Feijo on 14/10/2016. | |
// | |
import XCTest | |
class StringDatesTests: XCTestCase { | |
func testAsDate() { | |
let date = "01/01/2001".asDate(withFormat: "dd/MM/yyyy")! | |
XCTAssert(date.day == 1) | |
XCTAssert(date.month == 1) | |
XCTAssert(date.year == 2001) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment