Calculating date and time based on user's timezone [duplicate] - ios

This question already has answers here:
iOS: Convert UTC NSDate to local Timezone
(15 answers)
Closed 6 years ago.
I have a requirement in which I have a local DB full of dates from a particular timezone and I want to convert this date from that particular timezone to user's local timezone. The way I've implemented is that first I am always converting the DB date to UTC by adding the hours difference and then converting it to user local time zone by taking the time interval using
NSTimeInterval(NSTimeZone.localTimeZone().secondsFromGMT
and adding it to the UTC date. I would like to know that is the approach fine because it worked till now on my limited testing. Also will it account for countries which has Day light saving currently active.
The complete code :
func calculateDate(model:EventModel) -> NSDate
{
let date = model.Date
let startTime = model.StartTime
let arrayForTime = startTime?.componentsSeparatedByString(":")
let arrayForDates = date?.componentsSeparatedByString("-")
let calender = NSCalendar(identifier:NSCalendarIdentifierGregorian)
let year = Int(arrayForDates![2])
let month = Int(arrayForDates![1])
let day = Int(arrayForDates![0])
let hour = Int(arrayForTime![0])! + 3 //UTC - 3 the local time
let minutes = Int(arrayForTime![1])
let dateComponents = NSDateComponents()
dateComponents.day = day!
dateComponents.month = month!
dateComponents.year = year!
dateComponents.hour = hour
dateComponents.minute = minutes!
dateComponents.second = 0
dateComponents.timeZone = NSTimeZone(name: "UTC")
let UTCDate = calender?.dateFromComponents(dateComponents)
let dateLocal = self.getLocalDate(UTCDate!)
return dateLocal
}
func getLocalDate(utcDate:NSDate) -> NSDate
{
let timeInterval = NSTimeInterval(NSTimeZone.localTimeZone().secondsFromGMT)
let localdate = utcDate.dateByAddingTimeInterval(timeInterval)
return localdate
}
Previously I was using this but it was not returning the correct local date for Day light saving countries.
func getLocalDate(utcDate:NSDate) -> NSDate
{
let timeInterval = NSTimeInterval(NSTimeZone.localTimeZone().secondsFromGMT)
// let timeZoneObj = NSTimeZone.localTimeZone()
let localdate = utcDate.dateByAddingTimeInterval(timeInterval)
// let isDayLightSavingOn = timeZoneObj.isDaylightSavingTimeForDate(localdate)
// if(isDayLightSavingOn == true)
// {
// let dayLightTimeInterval = timeZoneObj.daylightSavingTimeOffsetForDate(localdate)
// timeInterval -= dayLightTimeInterval
// }
// localdate = utcDate.dateByAddingTimeInterval(timeInterval)
return localdate
}
Thanks in advance. Cheers !

You don't "convert" the NSDate to a timezone. You simply create a string representation of the NSDate in the user's time zone using a NSDateFormatter. The timeZone of the NSDateFormatter defaults to the user's own timezone, so no adjustments are necessary when displaying it in the user's local timezone. For example:
let date = ... // let's imagine it was "2016-05-18 00:03:34 +0000"
let formatter = NSDateFormatter()
formatter.dateStyle = .LongStyle
formatter.timeStyle = .LongStyle
let userDateString = formatter.stringFromDate(date)
If the user was in GMT/UTC-7 timezone, for example, that would result in:
"May 17, 2016 at 5:03:34 PM PDT"
Clearly, change the dateStyle and timeStyle to format it however best for your user interface. But don't do any adjustments of NSDate objects at all, but rather just build a string representation in the user's own timezone.

Related

Timezone in swift [duplicate]

This question already has answers here:
How can I parse / create a date time stamp formatted with fractional seconds UTC timezone (ISO 8601, RFC 3339) in Swift?
(13 answers)
Swift - Get local date and time
(11 answers)
Closed 12 months ago.
I am struggling quite a bit with dates. I have the following code:
Current Date in Amsterdam: 22-Februari-2022 - 11:40
Current Date in New York: 22-Februari-2022 - 05:40
The dateBoughtString goes in as follows: 2022-02-18T19:50:47.081Z
The current date is just the current date.
let dateFormatterNew = DateFormatter()
dateFormatterNew.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
dateFormatterNew.timeZone = TimeZone(abbreviation: "GMT+1:00")
dateFormatterNew.locale = Locale(identifier: "nl-NL")
let dateBoughtTemp = dateFormatterNew.date(from: positionStatsString[0])!
print(dateBoughtTemp) // Prints: 2022-02-18 18:50:47 +0000
dateFormatterNew.timeZone = TimeZone(abbreviation: "GMT-5:00")
dateFormatterNew.locale = Locale(identifier: "en_US")
let dateNowTemp = dateFormatterNew.string(from: Date())
let dateBoughtTempTwo = dateFormatterNew.string(from: dateBoughtTemp)
print(dateNowTemp) // Prints: 2022-02-22T05:41:49.973Z
print(dateBoughtTempTwo) // Prints: 2022-02-18T13:50:47.081Z
let dateNow = dateFormatterNew.date(from: dateNowTemp)
let dateBought = dateFormatterNew.date(from: dateBoughtTempTwo)
print(dateNow!) // Prints: 2022-02-22 10:41:49 +0000 **INCORRECT**
print(dateBought!) // Prints: 2022-02-18 18:50:47 +0000 **INCORRECT**
When I convert to string all seems fine and it works as it should.
But when I convert those strings back to a date they just go back to Amsterdam time with the current date even being one hour off.
What am I missing here?
The problem is in your's parameter 'Z':
'' means that it's content doesn't involved in time formatting.
So when you apply timeZone parameter date is printed in particular time zone without correct timeZone suffix and when it's scanned it's scanned in particular time zone, just expecting that there will by Z character at the end. So when you are formatting to date and then to string you are accumulating error caused by timezone difference.
Correct format will be "yyyy-MM-dd'T'HH:mm:ss.SSSXXX" or better to use ISO8601DateFormatter because you can't set invalid format in it.
So your printed dates will have valid timezone suffix and timezone suffix will be considered in backward conversion.
Another moment: you shouldn't convert string back to date with localized formatter, if it's UI part, but for that you can use UIDatePicker instead of UITextField.
So full code will be:
let isoDateFormatter = ISO8601DateFormatter()
isoDateFormatter.formatOptions = [.withInternetDateTime, .withFractionalSeconds]
let date = isoDateFormatter.date(from: "2022-02-18T19:50:47.081Z")!
let now = Date()
do {
let amsterdamDateFormatter = DateFormatter()
amsterdamDateFormatter.timeZone = .init(abbreviation: "GMT+1:00")
amsterdamDateFormatter.dateStyle = .long
amsterdamDateFormatter.timeStyle = .short
print("now in Amsterdam: \(amsterdamDateFormatter.string(from: now))")
print("time in Amsterdam: \(amsterdamDateFormatter.string(from: date))")
}
do {
let newYourkDateFormatter = DateFormatter()
newYourkDateFormatter.timeZone = .init(abbreviation: "GMT-5:00")
newYourkDateFormatter.dateStyle = .long
newYourkDateFormatter.timeStyle = .short
print("now in NY: \(newYourkDateFormatter.string(from: now))")
print("time in NY: \(newYourkDateFormatter.string(from: date))")
}
Use the below code for formatter, Change the timezone and dateFormat according to your need:
let formatter = DateFormatter()
formatter.calendar = Calendar(identifier: .iso8601)
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.timeZone = TimeZone(secondsFromGMT: 0)
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSXXXXX"

How to calculate time (minutes) between two dates in swift?

What do we got: Date+time (format yyyy-mm-dd hh:mm a)
What are we looking for: Time difference in minutes
What operation: NewDate - OldDate
So, I wonder how I could accomplish above goal? I would like to format the date and time to US, regardless from which locale the user has. How can I do that?
Then I will save the 'oldTime' into UserDefaults, and use it for later calculation. The goal is to put the user on delay for 5 minutes and the calculations will be performed to determine if user should be on delay or not.
Just make a function that takes two dates and compares them like this.
import UIKit
func minutesBetweenDates(_ oldDate: Date, _ newDate: Date) -> CGFloat {
//get both times sinces refrenced date and divide by 60 to get minutes
let newDateMinutes = newDate.timeIntervalSinceReferenceDate/60
let oldDateMinutes = oldDate.timeIntervalSinceReferenceDate/60
//then return the difference
return CGFloat(newDateMinutes - oldDateMinutes)
}
//Usage:
let myDateFormatter = DateFormatter()
myDateFormatter.dateFormat = "yyyy-MM-dd HH:mm"
//You'll need both dates to compare, you can get them by just storing a Date object when you first start the timer.
//Then when you need to check it, compare it to Date()
let oldDate: Date = myDateFormatter.date(from: String("2019-06-22 11:25"))
func validateRefresh() {
//do the comparison between the old date and the now date like this.
if minutesBetweenDates(oldDate, Date()) > 5 {
//Do whatever
}
}
You can, of course, change the .dateFormat value on the date formatter to be whatever format you'd like. A great website for finding the right format is: https://nsdateformatter.com/.
You say:
I would like to format the date and time to US, regardless from which locale the user has. How can I do that?
Specify a Locale of en_US_POSIX:
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd hh:mm a"
formatter.locale = Locale(identifier: "en_US_POSIX")
The locale is not the only question.
There’s also a timezone question. For example, you're driving out of Chicago and go from Central to Eastern timezones; do you really want to consider that one hour has passed?
Do you really want to discard seconds? If you do that, the 59 seconds between going from 8:00:00pm to 8:00:59pm will be considered “zero minutes” but the one second between 8:00:59pm and 8:01:00pm will be considered “one minute”.
Frankly, if I wanted to save a locale and timezone invariant date string, I’d suggest using ISO8601DateFormatter.
Then I will save the 'oldTime' into UserDefaults, and use it for later calculation.
If that’s why you’re using this DateFormatter, I’d suggest saving the Date object directly.
UserDefaults.standard.set(oldTime, forKey: "oldTime")
And to retrieve it:
if let oldTime = UserDefaults.standard.object(forKey: "oldTime") as? Date {
...
}
In terms of calculating the number of minutes between two Date objects
let minutes = Calendar.current
.dateComponents([.minute], from: date1, to: date2)
.minute
If you want the number of seconds, you can also use timeIntervalSince:
let seconds = date2.timeIntervalSince(date1)
And if you wanted to show the amount of elapsed time as a nice localized string:
let intervalFormatter = DateComponentsFormatter()
intervalFormatter.allowedUnits = [.minute, .second]
intervalFormatter.unitsStyle = .full
let string = intervalFormatter.string(from: date1, to: date2)
I'm not convinced that your question is the best way to go about accomplishing your aim, but the code below will work.
let dateFormatterNow = DateFormatter()
dateFormatterNow.dateFormat = "yyyy-MM-dd hh:mm a"
dateFormatterNow.timeZone = TimeZone(abbreviation: "EST")
let oldDateString = "2019-06-23 12:44 p"
let oldDate = dateFormatterNow.date(from: oldDateString)
let newDateString = "2019-06-23 12:54 p"
let newDate = dateFormatterNow.date(from: newDateString)
if let oldDate = oldDate, let newDate = newDate {
let diffInMins = Calendar.current.dateComponents([.minute], from: oldDate, to: newDate).minute
print(diffInMins)
}

getting wrong time while set date and time in one nsdate separately in ios

when i'm going set event from app to device calendar. i got wrong time.
i have three date picker one for date and other two for start time and end time for event. i set start date as end date in EKEvent because i have to set event on that day only.
get date from date-picker and store it as startdate and end date as nsdate type. below is my date-picker method
func pickerDate()
{
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "YYYY-MM-dd"
routineStartDate = dateFormatter.string(from: self.startDatePicker.date)
// it is for database entry in string and i get right string
print(routineStartDate)
startDate = self.startDatePicker.date as NSDate
print(startDate)
endDate = startDate
}
below method is for start time where i get time and convert to Time Interval and set it to start date.
func starttime() {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "HH:mm"
let then: Date? = self.startTimePicker.date
let difference: TimeInterval? = then?.timeIntervalSinceNow
startDate.addingTimeInterval(difference!)
routineStartTime = dateFormatter.string(from: self.startTimePicker.date)
// it is for database entry in string and i get right string
print(routineStartTime)
}
below method is for end time where i get time from picker and convert to Time Interval and set Time Interval to enddate
func endtime() {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "HH:mm"
routineEndTime = dateFormatter.string(from: self.endTimePicker.date)
print(routineEndTime)
// it is for database entry in string and i get right string
let then: Date? = self.endTimePicker.date
let difference: TimeInterval? = then?.timeIntervalSinceNow
endDate.addingTimeInterval(difference!)
}
below image showing which date i set in picker
below is My EKEvent method where i create event.
existevent.title = tempDescription
existevent.startDate = startDate as Date
existevent.endDate = endDate as Date
existevent.isAllDay = false
existevent.notes = "This is a note"
existevent.calendar = cal
when i check event in calendar i got Problem, i get wrong time in event.i set start time 12:50 pm end time 1:50 pm on date 27 june 2017 in caledar app. date is set perfectly but why time is not set perfectly ? below image for calendar app.
i have doubt in conversion of time interval and set to date. but what i missing dont know.
please suggest me solution and ideas to solve.
Thank you
you need to convert the time to the desired time zone. Because now the date is set correctly in your timezone, but is displayed in +0000 Screenshot. Use calendar for date representation this
And change your code like this in both methods:
startDate.addingTimeInterval(difference!)
to
self.startDate = startDate.addingTimeInterval(difference!)
and
endDate.addingTimeInterval(difference!)
to
self.endDate = endDate.addingTimeInterval(difference!)
in your case Xcode Warning "Result of call to 'addingTimeInterval' is unused"
Try to convert date, before set it to you "existevent", or when you show it
func convertDate(date:Date) -> Date {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "HH:mm" // or other format
var comp = DateComponents()
let calendar = Calendar.current
comp.hour = Calendar.current.component(.hour, from: date)
comp.minute = Calendar.current.component(.minute, from: date)
comp.timeZone = TimeZone(abbreviation: "GMT")!
return calendar.date(from: comp)!
}

apply DayLightSavingTime to NSDate

I get a NSDate from a UIDatePicker (timePicker) which is converted to the users time zone but it is one hour off because I'm in Summer Time.
So I tried to check if the User is in Summer Time if yes it should add the time to the NSDate/dateInUserTimezone.
How can I achieve this?
func convertTimeZone() ->NSDate {
var date = timePicker.date
print(date)
var formatter = NSDateFormatter()
let timeZone = NSTimeZone(abbreviation: userTimeZone())
formatter.timeZone = timeZone
formatter.dateFormat = "HH:mm:ss"
var isDaylightSavingTime: Bool {
return NSTimeZone.localTimeZone().daylightSavingTime
}
print(isDaylightSavingTime)
var daylightSavingTimeOffset: NSTimeInterval {
return NSTimeZone.localTimeZone().daylightSavingTimeOffset
}
let dateInUserTimezone = formatter.dateFromString(formatter.stringFromDate(date))
return dateInUserTimezone!
}
func userTimeZone() -> String {
return NSTimeZone.localTimeZone().abbreviation ?? ""
}
NSDate is inherently in UTC (it has no timezone). Best to save/transfer your dates in UTC. Then, you can convert this UTC time stamp to any time zone of your preference. Instead of detecting whether the user is DST or not, try converting to your preferred time zone. Here's an example of how you would convert any date to local time zone.
var date = ...
var myTimeZone = NSTimeZone.localTimeZone()
var df = NSDateFormatter()
df.timeZone = myTimeZone
df.dateFormat = "yyyy.MM.dd G 'at' HH:mm:ss zzz"
df.stringFromDate(date)

Hijri (islamic) calendar in swift التاريخ الهجري

can someone help me please
I'm trying to create an IOS app using Swift language and I need to use Hijri (islamic) calendar
I tried many time but I failed :(
this is my try
let datenow = NSDate()
let calendar = NSCalendar.currentCalendar()
let components = calendar.components(NSCalendarUnit(UInt.max), fromDate: datenow)
var gregorian = NSCalendar(identifier:NSIslamicCivilCalendar)
var date = gregorian.dateFromComponents(components)
println(date)
and the output is wrong
2576-04-25 09:05:08 +0000
We are in year 1434 in hijri not 2576 !
You're mixing up the calendars, dates & components:
let datenow = NSDate()
// This is a point in time, independent of calendars
let calendar = NSCalendar.currentCalendar()
// System calendar, likely Gregorian
let components = calendar.components(NSCalendarUnit(UInt.max), fromDate: datenow)
// Gregorian components
println("\(components.year)") // "2014"
var islamic = NSCalendar(identifier:NSIslamicCivilCalendar)! // Changed the variable name
// *** Note also NSCalendar(identifier:) now returns now returns an optional ***
var date = islamic.dateFromComponents(components)
// so you have asked to initialise the date as AH 2014
println(date)
// This is a point in time again, sometime in AH 2014, or AD 2576
What you need to do is simply:
let datenow = NSDate()
let islamic = NSCalendar(identifier:NSIslamicCivilCalendar)!
let components = islamic.components(NSCalendarUnit(UInt.max), fromDate: datenow)
println("Date in system calendar:\(datenow), in Hijri:\(components.year)-\(components.month)-\(components.day)")
//"Date in system calendar:2014-09-25 09:53:00 +0000, in Hijri:1435-11-30"
To get a formatted string, rather than just the integer components, you need to use NSDateFormatter, which will allow you to specify the calendar & date as well as the format. See here.
Update
To simply transliterate the numerals to (Eastern) Arabic numerals (as 0...9 are referred to as (Western) Arabic numerals to distinguish them from, say, Roman numerals), as requested, you could use:
let sWesternArabic = "\(components.day)-\(components.month)-\(components.year)"
let substituteEasternArabic = ["0":"٠", "1":"١", "2":"٢", "3":"٣", "4":"٤", "5":"٥", "6":"٦", "7":"٧", "8":"٨", "9":"٩"]
var sEasternArabic = ""
for i in sWesternArabic {
if let subs = substituteEasternArabic[String(i)] { // String(i) needed as i is a character
sEasternArabic += subs
} else {
sEasternArabic += String(i)
}
}
println("Western Arabic numerals = \(sWesternArabic), Eastern Arabic numerals = \(sEasternArabic)")
Get Hajri Date:
Swift :
let dateFor = DateFormatter()
let hijriCalendar = Calendar.init(identifier: Calendar.Identifier.islamicCivil)
dateFor.locale = Locale.init(identifier: "ar") // or "en" as you want to show numbers
dateFor.calendar = hijriCalendar
dateFor.dateFormat = "dd/MM/yyyy"
print(dateFor.string(from: Date()))
Obj-C :
NSCalendar *hijriCalendar2 = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierIslamicCivil];
NSDateFormatter * lastDate = [[NSDateFormatter alloc]init];
lastDate.calendar = hijriCalendar2;
[lastDate setDateFormat:#"dd/MM/yyyy HH:mm:ss"];
NSString * dateString = [lastDate stringFromDate:[NSDate date]];
thank you #Grimxn and I also find a great way to show arabic numbers
let formatter = NSNumberFormatter()
formatter.locale = NSLocale(localeIdentifier: "ar_SA")
var date:String = "\(formatter.stringFromNumber(components.year))-\(formatter.stringFromNumber(components.month))-\(formatter.stringFromNumber(components.day))"
it will print ١-١١-١٤٣٤
Swift 3 & 4
let dateNow = DateFormatter()
let islamicCalendar = Calendar.init(identifier: Calendar.Identifier.islamicCivil)
dateNow.calendar = islamicCalendar
dateNow.locale = Locale.init(identifier: "ar_SA") // This is used to show numbers, day and month in arabic letters
dateNow.dateFormat = "EEE dd MMM yyyy"
print("\(dateNow.string(from: Date()))")
find out and place date format from:
http://userguide.icu-project.org/formatparse/datetime
Swift 5:
let hijriCalendar = Calendar(identifier: .islamicCivil)
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "ar")
formatter.calendar = hijriCalendar
formatter.dateFormat = "dd/MM/yyyy"
print(formatter.string(from: Date()))

Resources