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
| var cmp = DateComponents() | |
| cmp.timeZone = TimeZoneName.europeOslo.timeZone | |
| cmp.calendar = CalendarName.gregorian.calendar | |
| cmp.calendar?.locale = LocaleName.englishNorway.locale | |
| cmp.year = 2005 | |
| cmp.month = 4 | |
| cmp.day = 15 | |
| cmp.hour = 20 | |
| cmp.minute = 30 |
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
| let c: [Calendar.Component : Int] = [.year: 2002, .month: 3, .hour: 5, .day: 4, .minute: 6, .second: 7, .nanosecond: 87654321] | |
| // create a new DateInRegion on 2002-03-04 at 05:06:07.87654321 (+/-10) in Region.Local() | |
| let date = try! DateInRegion(components: c, fromRegion: nil) |
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
| let rome = Region(tz: TimeZoneName.europeRome, cal: CalendarName.gregorian, loc: LocaleName.italian) | |
| // Parse a string which a custom format | |
| let date1 = try! DateInRegion(string: "1999-12-31 23:30:00", format: .custom("yyyy-MM-dd HH:mm:ss"), fromRegion: regionRome) | |
| // Parse an AltRSS date from string using String extension | |
| let date2 = try! "3 feb 2001 15:30:00 +0100".date(format: .rss(alt: true), fromRegion: regionRome) | |
| // ...it's the same of: | |
| let date3 = try! DateInRegion(string: "3 feb 2001 15:30:00 +0100", format: .ss(alt: true)), fromRegion: regionRome) |
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
| // Create a new date in Rome on 2015-01-03 @ 15:10:00 | |
| let regionRome = Region(tz: TimeZoneName.europeRome, cal: CalendarName.gregorian, loc: LocaleName.italianItaly) | |
| var date = try! DateInRegion(components: [.year: 2015, .month: 1, .day: 3, .hour: 15, .minute: 10, .second: 0], fromRegion: regionRome) | |
| // Let's go with some math operations | |
| date = date + 3.seconds + 15.minutes // -> 03 gen 2015, 15:25:03 CET | |
| date = date - 120.seconds // minus 2 minutes -> 03 gen 2015, 15:23:03 CET | |
| date = date + 3.weeks // 24 gen 2015, 15:23:03 CET | |
| // you can also add an absolute amount of seconds and it will modify directly the date |
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
| // 03 gen 2015, 15:10:00 CET | |
| let date1 = try! DateInRegion(components: [.year: 2015, .month: 1, .day: 3, .hour: 15, .minute: 10, .second: 0], fromRegion: regionRome) | |
| let date2 = try! DateInRegion(components: [.year: 2015, .month: 2, .day: 5, .hour: 15, .minute: 10, .second: 0], fromRegion: regionRome) | |
| if date2 > date1 { // DateInRegion supports Equatable protocol you can use >=, <=, < or > | |
| print("'\(date2)' date is later than '\(date1)'") | |
| } | |
| if date2 == date1 { | |
| // simple equality |
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
| // Create a Region in Rome TimeZone with Gregorian Calendar and Italy locale settings | |
| let regionRome = Region(tz: TimeZoneName.europeRome, cal: CalendarName.gregorian, loc: LocaleName.italianItaly) | |
| // 03 gen 2015, 15:10:00 CET | |
| let date = try! DateInRegion(components: [.year: 2015, .month: 1, .day: 3, .hour: 15, .minute: 10, .second: 0], fromRegion: regionRome) | |
| let startOfHour = date.startOf(component: .hour) // 03 gen 2015, 15:00:00 CET | |
| let startOfMonth = date.startOf(component: .month) // 01 gen 2015, 00:00:00 CET | |
| let startOfWeek = date.startOf(component: .weekOfMonth) // 28 dic 2014, 00:00:00 CET |
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
| // Create a Region in Rome TimeZone with Gregorian Calendar and Italy locale settings | |
| let regionRome = Region(tz: TimeZoneName.europeRome, cal: CalendarName.gregorian, loc: LocaleName.italianItaly) | |
| // 03 gen 2015, 15:10:00 CET | |
| let date = try! DateInRegion(components: [.year: 2015, .month: 1, .day: 3, .hour: 15, .minute: 10, .second: 0], fromRegion: regionRome) | |
| let startOfHour = date.endOf(component: .hour) // 03 gen 2015, 15:59:59 CET | |
| let startOfMonth = date.endOf(component: .month) // 31 gen 2015, 23:59:59 CET | |
| let startOfWeek = date.endOf(component: .weekOfMonth) // 03 gen 2015, 23:59:59 CET |
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
| // Create a Region in Rome TimeZone with Gregorian Calendar and Italy locale settings | |
| // 03 gen 2015, 15:10:00 CET | |
| let regionRome = Region(tz: TimeZoneName.europeRome, cal: CalendarName.gregorian, loc: LocaleName.italianItaly) | |
| let date = try! DateInRegion(components: [.year: 2015, .month: 1, .day: 3, .hour: 15, .minute: 10, .second: 0], fromRegion: regionRome) | |
| // Set custom time | |
| let newDateAtGivenTime = try! date.atTime(hour: 09, minute: 10, second: 22) // 03 gen 2015, 09:10:22 CET |
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
| // Create a Region in Rome TimeZone with Gregorian Calendar and Italy locale settings | |
| // 03 gen 2015, 15:10:00 CET | |
| let regionRome = Region(tz: TimeZoneName.europeRome, cal: CalendarName.gregorian, loc: LocaleName.italianItaly) | |
| let date = try! DateInRegion(components: [.year: 2015, .month: 1, .day: 3, .hour: 15, .minute: 10, .second: 0], fromRegion: regionRome) | |
| let atGivenYear = try! date.at(unit: .year, value: 2020) // 01 gen 2020, 00:00:00 CET. day is altered |
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
| // Create a Region in Rome TimeZone with Gregorian Calendar and Italy locale settings | |
| // 03 gen 2015, 15:10:00 CET | |
| let regionRome = Region(tz: TimeZoneName.europeRome, cal: CalendarName.gregorian, loc: LocaleName.italianItaly) | |
| let date = try! DateInRegion(components: [.year: 2015, .month: 1, .day: 3, .hour: 15, .minute: 10, .second: 0], fromRegion: regionRome) | |
| let time = try! date.at(unitsWithValues: [.year: 2016, .month: 7]) // 01 gen 2016, 00:00:00 CET |