EST date timezone doesn't add up hours properly swift 5 - swift

I am trying to add up to 2 hours to the current time. When it's "Asia/Kolkata" the following function works, but if it's "America/Toronto" it reduces the time by 2 hours instead of adding. Please see the below code and shade some light where I am wrong. TIA
let now = Date().localDate() // 2021-03-16 06:24:44 +0000 //extension of localDate
let modifiedDate = Calendar.current.date(byAdding: .hour, value: 2, to: now)!
let hour = Calendar.current.dateComponents([.hour], from: modifiedDate, to: now).hour //-2
let formatter = DateFormatter()
let timezone = TimeZone.current.identifier
formatter.timeZone = TimeZone(abbreviation: "America/Toronto")
formatter.locale = Calendar.current.locale
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss ZZZ"
self.dateString = formatter.string(from: modifiedDate)
print("Date: ", self.dateString) //2021-03-16 04:24:44 -0400
extension Date {
func localDate() -> Date {
let nowUTC = Date()
let timeZoneOffset = Double(TimeZone.current.secondsFromGMT(for: nowUTC))
guard let localDate = Calendar.current.date(byAdding: .second, value: Int(timeZoneOffset), to: nowUTC) else {return Date()}
return localDate
}
}

Your extension is totally wrong. There is no "nowUTC". Now is anywhere in the world. A date has no timezone it is just a point in time. You should never add/subtract the seconds from GMT to your Date() / now object
To make it easier to explain what is wrong there all you need is to use Date method description with locale and use current. Note also that adding seconds to a date will never fail so it is safe to safely force unwrap the result. The use of a guard there is pointless and don't return Date() when failing. You should change the return type to an optional Date and return nil but it is not necessary in this case:
Again the following extension is what you should never do
extension Date {
func localDate() -> Date {
let nowUTC = Date()
let timeZoneOffset = Double(TimeZone.current.secondsFromGMT(for: nowUTC))
return Calendar.current.date(byAdding: .second, value: Int(timeZoneOffset), to: nowUTC)!
}
}
Now it is 13:06 in Rio de Janeiro Brazil and your date returned by the localDate method is "Tuesday, March 16, 2021 at 10:06:13 AM Brasilia Standard Time"
Date().localDate().description(with: .current) // Tuesday, March 16, 2021 at 10:06:13 AM Brasilia Standard Time"
If you need "local date" all you need is to use Date(). If you would like to display the local date you need to use DateFormatter. When you print the date object it will always show the UTC time (+0000). If you want to print the local date just print the description with the current locale.
let now = Date()
now.description(with: .current) // "Tuesday, March 16, 2021 at 1:06:13 PM Brasilia Standard Time"

Related

How to change the hours and minutes in an existing Date object in Swift?

I need to compare two Date object to get the day difference between them, for example: 10/10 compares with today 10/7 will be 3, but the Date object returned to me from server is not aligned with the current time. There will be a few minutes difference which results in 10/10 being 2 days ahead of 10/7 because of the delay
I found a line of code that can give me a Date object of the current time, but I want to convert an existing Date object from somewhere else, how do I do it?
let today = Calendar.current.date(bySettingHour: 0, minute: 0, second: 0, of: Date())!
e.g. 2020-10-08 16:08:57.259580+0000 I want it to be 2020-10-08 00:00:00 +0000 something like this
Don’t use midnight. Just parse your date string first. For calendrical calculations you should always use noon. First create a custom date format to parse your date string properly.
extension Formatter {
static let iso8601: DateFormatter = {
let formatter = DateFormatter()
formatter.locale = .init(identifier: "en_US_POSIX")
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss.SSSSSSxx"
formatter.timeZone = TimeZone(secondsFromGMT: 0)
return formatter
}()
}
Then create a helper to convert your date to noon time and another one to calculate the days between two dates and set them to noon:
extension Date {
var noon: Date {
Calendar.current.date(bySettingHour: 12, minute: 0, second: 0, of: self)!
}
func days(from date: Date) -> Int {
Calendar.current.dateComponents([.day], from: date.noon, to: noon).day!
}
var daysFromToday: Int { days(from: Date()) }
}
Playground testing:
let dateString = "2020-10-08 16:08:57.259580+0000"
let now = Date() // "Oct 8, 2020 at 5:56 AM"
print(Formatter.iso8601.string(from: now)) // "2020-10-08 08:56:46.179000+0000\n"
if let date = Formatter.iso8601.date(from: dateString) { // "Oct 8, 2020 at 1:08 PM"
let days = Date().days(from: date) // 0
}
let dateString = "2020-10-10 16:08:57.259580+0000"
if let date = Formatter.iso8601.date(from: dateString) {
let days = date.daysFromToday // 2
}

Joining separated Date and Hour to create a single Date() Object

I have this app written in swift where I get a future date and a future hour (As Unix Timestamp) separately and I want to turned them into one Date() Object so I can converted to Unix Timestamp.
If you now another way to converted directly to Unix Timestamp feel free to post.
This may give you some ideas on how to accomplish what you want:
let calendar = Calendar.current
let currentDate = Date()
// Random future date, 1 month from now
let futureDate = calendar.date(byAdding: .month, value: 1, to: currentDate)!
let futureDateComponents = calendar.dateComponents([.year, .month, .day], from: futureDate)
// Random hour
let futureHour = 2
// Use your future date and your future hour to set the components for the new date to be created
var newDateComponents = calendar.dateComponents([.year, .month, .day, .hour, .minute, .second], from: currentDate)
newDateComponents.year = futureDateComponents.year
newDateComponents.month = futureDateComponents.month
newDateComponents.day = futureDateComponents.day
newDateComponents.hour = futureHour
newDateComponents.minute = 0
newDateComponents.second = 0
// Create a new date from components
let newDate = calendar.date(from: newDateComponents)!
// Convert new date to unix time format
let unixTime = newDate.timeIntervalSince1970
print(newDate) // 2018-03-06 02:00:00 +0000
print(unixTime) // 1520301600.0
Note that in a real project you should avoid force unwrapping (i.e. using '!').
If you have a date string in the form of dd-MM-yyyy, you can convert that to a Date object like so:
let string = "02-06-2018"
let formatter = DateFormatter()
formatter.dateFormat = "dd-MM-yyyy"
formatter.locale = Locale(identifier: "en_US_POSIX") // this is only needed if string is always gregorian but you don't know what locale the device is using
guard let date = formatter.date(from: string) else {
// handle error however you'd like
return
}
Or, if you already have a Date object, then you don't need the above. But regardless, once you have a Date, you can then get a Date by setting the hour, minute, and second as follows:
let hour = 14 // 2pm
guard let result = Calendar.current.date(bySettingHour: hour, minute: 0, second: 0, of: date) else {
// handle error however you'd like
return
}

Take a certain amount of days off the current date and print with certain date format

How would I take a certain amount of days (take away one day) off the current date and print with certain date format to the console.
I'm currently using:
print((Calendar.current as NSCalendar).date(byAdding: .day, value: -1, to: Date(), options: [])!)
Which prints the date and time as:
yyyy-MM-dd HH:MM:SS +0000
But I want it to print like:
dd-MM-yyyy
Is this at all possible?
It's best to break that into a few more easily readable/maintainable lines. First, calculate the date you want, then apply a date formatter.
let yesterday = Calendar.current.date(byAdding: .day, value: -1, to: Date())
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy"
print(dateFormatter.stringFromDate(yesterday))
swift 3.0 version
let yesterday = Calendar.current.date(byAdding: .day, value: -1, to: Date())
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy"
if let yesterday = yesterday {
print(dateFormatter.string(from: yesterday))
}else{
print("Date incorrectly manipulated")
}
You shouldn't use fixed format for Date format strings. Because you might have users from around the world and they don't see dates in the same way.
Rather you should use template format. You just specify which components you want to show and their order like:
let dateFormatter = DateFormatter()
// dateFormatter.locale = Locale(identifier: "bn-BD") // Enable this line only at the time of your debugging/testing
dateFormatter.dateFormat = DateFormatter.dateFormat(fromTemplate: "ddMMyyyy",
options: 0,
locale: dateFormatter.locale)
let date = Calendar.current.date(byAdding: .day, value: -1, to: Date())
if let date = date {
let dateString = dateFormatter.string(from: date)
print(dateString)
}
You shouldn't set your locale by yourself. It's done by the
framework. You should only set your locale manually only at the time
of testing your app.
In the above example I'm using Locale as "bn-BD" which means Bangla in Bangladesh. You can find your a list of locales here

NSDate which is returned is incorrect

I am build a function to get the date for the Chinese new year from the date which is passed from a datePicker. the convertDateFormater converts a string formatted date to a date object. the date which i get back is
Returned date = "03-Feb-2030" for the year "1970-05-22"
but if i run the current date with "NSDate() as Date" i get the right output. for the year 2016.
func convertDateFormater(date: String) -> Date
{
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"//this your string date format
dateFormatter.timeZone = NSTimeZone(name: "UTC") as TimeZone!
let date = dateFormatter.date(from: date)
return date!
}
var inputDate1 = convertDateFormater(date: "1970-05-22")
print(inputDate1)
let chineseCalendar = NSCalendar(calendarIdentifier: NSCalendar.Identifier.chinese)!
let formatter: DateFormatter = {
let fmt = DateFormatter()
fmt.dateStyle = .full
fmt.timeStyle = .full
fmt.dateFormat = "dd-MMM-yyyy"
return fmt
}()
var comps = chineseCalendar.components([.year], from: inputDate)
guard let newYear = chineseCalendar.date(from: comps) else { fatalError("no date for \(comps.year)")}
let getNewYear = formatter.string(from: newYear)
print("\(NSDate() as Date) : \(getNewYear) - \(newYear)")
In the Gregorian calendar, there are two eras: the current "Common Era" (abbreviated "CE" and also known as "Anno Domini" or "AD") and "Before Common Era" (abbreviated "BCE" and also known as "Before Christ" or "BC"). So a year number on the Gregorian calendar is ambiguous: "1970" by itself can mean either "1970 CE" or "1970 BCE". We usually assume CE when no era is specified.
I know almost nothing about the Chinese calendar, but I know it has many more eras than the Gregorian calendar, so a year number on the Chinese calendar is even more ambiguous. The code you posted doesn't do anything about eras. So I copied and pasted your code into a playground and made one change:
var comps = chineseCalendar.components([.era, .year], from: inputDate)
// ^^^^^^
The output:
1970-05-22 00:00:00 +0000
2016-11-30 01:40:12 +0000 : 06-Feb-1970 - 1970-02-06 06:00:00 +0000
Indeed, 1970-02-06 was the date of the Chinese new year in 1970 CE on the Gregorian calendar.

How do I find the beginning of the week from an NSDate?

I'm implementing a calendar view, and I'd like it to start at the beginning of the week containing a particular date. Eg. If the target date is Monday, Feb 29, 2016, and the current calendar is set to start on Sunday, I'd like my view to start with Sunday, February 28.
This seems like it should be straightforward:
let calendar = NSCalendar.currentCalendar()
let firstDate = calendar.nextDateAfterDate(targetDate,
matchingUnit: .Weekday,
value: calendar.firstWeekday,
options: .SearchBackwards)
But this fails with:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Exactly one option from the set {NSCalendarMatchPreviousTimePreservingSmallerUnits, NSCalendarMatchNextTimePreservingSmallerUnits, NSCalendarMatchNextTime} must be specified.'
I can get basically what I want with:
let firstDate = calendar.nextDateAfterDate(firstDate,
matchingUnit: .Weekday,
value: calendar.firstWeekday,
options: .MatchPreviousTimePreservingSmallerUnits)?
.dateByAddingTimeInterval(-7 * 84600)
But it seems like a bad practice, since sometimes the number of seconds in a day isn't 86400.
Is there a better way?
you can use Calendar method date(from: DateComponents) passing [.yearForWeekOfYear, .weekOfYear] components from any date it will return the first day of the week from the calendar used. So if you would like to get Sunday just use Gregorian calendar. If you would like to get the Monday as the first day of the week you can use Calendar .iso8601 as you can see in this answer
Xcode 12 • Swift 5.3 or later (works with previous Swift versions as well)
extension Calendar {
static let gregorian = Calendar(identifier: .gregorian)
}
extension Date {
func startOfWeek(using calendar: Calendar = .gregorian) -> Date {
calendar.dateComponents([.calendar, .yearForWeekOfYear, .weekOfYear], from: self).date!
}
}
usage:
Date().startOfWeek() // "Sep 20, 2020 at 12:00 AM"
If you would like to get the beginning of week at a particular timezone you just need to use a custom calendar:
var gregorianUTC = Calendar.gregorian
gregorianUTC.timeZone = TimeZone(identifier: "UTC")!
print(Date().startOfWeek(using: gregorianUTC)) // "2020-09-20 00:00:00 +0000\n"
Swift 4 Solution
I have figured out according to my requirement, where I have find out dates for following.
1. Today
2. Tomorrow
3. This Week
4. This Weekend
5. Next Week
6. Next Weekend
So, I have created Date Extension to get Dates of Current Week and Next Week.
CODE
extension Date {
func getWeekDates() -> (thisWeek:[Date],nextWeek:[Date]) {
var tuple: (thisWeek:[Date],nextWeek:[Date])
var arrThisWeek: [Date] = []
for i in 0..<7 {
arrThisWeek.append(Calendar.current.date(byAdding: .day, value: i, to: startOfWeek)!)
}
var arrNextWeek: [Date] = []
for i in 1...7 {
arrNextWeek.append(Calendar.current.date(byAdding: .day, value: i, to: arrThisWeek.last!)!)
}
tuple = (thisWeek: arrThisWeek,nextWeek: arrNextWeek)
return tuple
}
var tomorrow: Date {
return Calendar.current.date(byAdding: .day, value: 1, to: noon)!
}
var noon: Date {
return Calendar.current.date(bySettingHour: 12, minute: 0, second: 0, of: self)!
}
var startOfWeek: Date {
let gregorian = Calendar(identifier: .gregorian)
let sunday = gregorian.date(from: gregorian.dateComponents([.yearForWeekOfYear, .weekOfYear], from: self))
return gregorian.date(byAdding: .day, value: 1, to: sunday!)!
}
func toDate(format: String) -> String {
let formatter = DateFormatter()
formatter.dateFormat = format
return formatter.string(from: self)
}
}
USAGE:
let arrWeekDates = Date().getWeekDates() // Get dates of Current and Next week.
let dateFormat = "MMM dd" // Date format
let thisMon = arrWeekDates.thisWeek.first!.toDate(format: dateFormat)
let thisSat = arrWeekDates.thisWeek[arrWeekDates.thisWeek.count - 2].toDate(format: dateFormat)
let thisSun = arrWeekDates.thisWeek[arrWeekDates.thisWeek.count - 1].toDate(format: dateFormat)
let nextMon = arrWeekDates.nextWeek.first!.toDate(format: dateFormat)
let nextSat = arrWeekDates.nextWeek[arrWeekDates.nextWeek.count - 2].toDate(format: dateFormat)
let nextSun = arrWeekDates.nextWeek[arrWeekDates.nextWeek.count - 1].toDate(format: dateFormat)
print("Today: \(Date().toDate(format: dateFormat))") // Sep 26
print("Tomorrow: \(Date().tomorrow.toDate(format: dateFormat))") // Sep 27
print("This Week: \(thisMon) - \(thisSun)") // Sep 24 - Sep 30
print("This Weekend: \(thisSat) - \(thisSun)") // Sep 29 - Sep 30
print("Next Week: \(nextMon) - \(nextSun)") // Oct 01 - Oct 07
print("Next Weekend: \(nextSat) - \(nextSun)") // Oct 06 - Oct 07
You can modify Extension according to your need.
Thanks!
You can implement this as Date class extension or something. It should returns something like 2020-01-06 00:00:00 +0000
Xcode 11.3 Swift 5
func firstDayOfWeek() -> Date {
var c = Calendar(identifier: .iso8601)
c.timeZone = TimeZone(secondsFromGMT: 0)!
print(
c.date(from: c.dateComponents([.weekOfYear, .yearForWeekOfYear], from: Date()))!
)
}
The Calendar has a mechanism for finding date at the start of a given time interval (say week of year, or month) that contains a given date:
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let date = dateFormatter.date(from: "2017-01-07")
if let date = date {
let calendar = Calendar(identifier: .gregorian)
var startDate : Date = Date()
var interval : TimeInterval = 0
if calendar.dateInterval(of: .weekOfYear, start: &startDate, interval: &interval, for: date) {
print("Start of week is \(startDate)")
// prints "Start of week is 2017-01-01 06:00:00 +0000"
}
}
In order to get the user's locale settings respected correctly, you should use the user's Calendar firstWeekday property in the DateComponents. This is what I usually use:
// MARK: first day of week
extension Date {
/**
Finds the first day of the week the subject date falls into.
- Parameter calendar: The calendar to use. Defaults to the user's current calendar.
- Returns: The `Date` of the first day of the week into which the subject date falls.
`startOfWeek()` respects the user's locale settings, i.e. will automatically use Sunday/Monday/etc. as first
weekday based on the user's region and locale settings.
*/
func startOfWeek(using calendar: Calendar = .current) -> Date? {
var components = calendar.dateComponents([.weekday, .year, .month, .weekOfYear], from: self)
components.weekday = calendar.firstWeekday
return calendar.date(from: components)
}
}
Basically use
NSCalender
and
dateByAddingComponents
. For solving of you're problem try to use this code sample:
let cal = NSCalendar.currentCalendar()
let components = NSDateComponents()
components.weekOfYear -= 1
if let date = cal.dateByAddingComponents(components, toDate: NSDate(), options: NSCalendarOptions(0)) {
var beginningOfWeek: NSDate?
var weekDuration = NSTimeInterval()
if cal.rangeOfUnit(.CalendarUnitWeekOfYear, startDate: &beginningOfWeek, interval: &weekDuration, forDate: date) {
print(beginningOfWeek)
}
}
I had problems with all previous solutions, since they do not take into account user's calendar setting. Next code will be taking into account that.
extension Date {
var startOfWeek: Date? {
let calendar = Calendar.current
var components: DateComponents? = calendar.dateComponents([.weekday, .year, .month, .day], from: self)
var modifiedComponent = components
modifiedComponent?.day = (components?.day ?? 0) - ((components?.weekday ?? 0) - 1)
return calendar.date(from: modifiedComponent!)
}
var endOfWeek: Date? {
let calendar = Calendar.current
var components: DateComponents? = calendar.dateComponents([.weekday, .year, .month, .day], from: self)
var modifiedComponent = components
modifiedComponent?.day = (components?.day ?? 0) + (7 - (components?.weekday ?? 0))
modifiedComponent?.hour = 23
modifiedComponent?.minute = 59
modifiedComponent?.second = 59
return calendar.date(from: modifiedComponent!)
}
}