Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions DateToolsSwift/DateTools/Date+Inits.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,16 @@ public extension Date {
* - parameter dateString: Date in the formatting given by the format parameter
* - parameter format: Format style using Apple's date formatting guide
* - parameter timeZone: Time zone of date
* - parameter calendar: The calendar to generate the date against. By default it is the current calendar
*/
public init(dateString: String, format: String, timeZone: TimeZone) {
public init(dateString: String, format: String, timeZone: TimeZone, calendar: Calendar = Calendar.current) {
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .none;
dateFormatter.timeStyle = .none;
dateFormatter.timeZone = timeZone;
dateFormatter.dateFormat = format;

dateFormatter.dateStyle = .none
dateFormatter.timeStyle = .none
dateFormatter.timeZone = timeZone
dateFormatter.dateFormat = format
dateFormatter.calendar = calendar

guard let date = dateFormatter.date(from: dateString) else {
self = Date()
return
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,14 @@ class DateDateToolsTests: XCTestCase {
let testDate = Date(year: 2011, month: 12, day: 25)
XCTAssertTrue(testDate.year == controlDate.year && testDate.month == controlDate.month && testDate.day == controlDate.day)
}


func testInitWithDatestringFormatTimezoneCalendar() {
let testDate = Date(dateString: "2011 12 25 4:30:30.000", format: "yyyy MM dd HH:mm:ss.SSS", timeZone: TimeZone(abbreviation: "CST")!, calendar: Calendar(identifier: .persian))
self.formatter.timeZone = TimeZone(abbreviation: "CST")
controlDate = self.formatter.date(from: "2633 03 16 4:30:30.000")!
XCTAssertTrue(testDate == controlDate)
}

func testInitWithDatestringFormatTimezone() {
let testDate = Date(dateString: "2011 12 25 4:30:30.000", format: "yyyy MM dd HH:mm:ss.SSS", timeZone: TimeZone(abbreviation: "CST")!)
self.formatter.timeZone = TimeZone(abbreviation: "CST")
Expand Down