Ios Swift Date Convert from TimeZone - ios

dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
dateFormatter.timeZone = TimeZone(abbreviation: "KST")
let nowstring = dateFormatter.string(from: Date())
let nowdate = dateFormatter.date(from: nowstring)
print(nowstring)
print(nowdate)
print this 2018-07-24T15:06:26
Optional(2018-07-24 06:06:26 +0000)
i want nowdate value 2018-07-24T15:06:26
but String to Date without timezone
I have to use to timeIntervalSince so nowdate is must have timezone

let today = NSDate()
print("\(today)")
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let nowDatestring = formatter.string(from: today)
formatter.timeZone = TimeZone(abbreviation: "KST")
let kstDate = formatter.date(from: nowDatestring)
print("\(kstDate!)")
formatter.timeZone = TimeZone(abbreviation: "PST")
let pstDate = formatter.date(from: nowDatestring)
print("\(pstDate!)")
formatter.timeZone = TimeZone(abbreviation: "IST")
let istDate = formatter.date(from: nowDatestring)
print("\(istDate!)")

Date is a specific point in time, which is independent of time zone. If you print a date value it will be always independent of any calendar or time zone. It will give same value for you(KST) and me(IST). You can specify your time zone when you format it using DateFormatter.

Related

Separate Time from Date String ios

How to separate hour and minute from date .
let StDate = "2021-04-03T10:15:00Z"
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
let dateFormatterPrint = DateFormatter()
dateFormatterPrint.dateFormat = "HH:mm"
if let date1 = formatter.date(from: StDate) {
let tme = dateFormatterPrint.string(from: date1)
print(tme)
}
Out Put
15:45
How to get 10:15
This most likely happens because you reside in a GMT+5 time zone. Meanwhile, your date string is UTC.
Use this to override the time zone:
dateFormatterPrint.timeZone = TimeZone(abbreviation: "UTC")
Your dateFormattedPrint by default uses your local timezone. Add
dateFormatterPrint.timeZone = TimeZone(identifier: "GMT")
to make it use GMT.

Swift datefomatter returns wrong time

I am working in iOS app with Dateformatter which is return wrong time. I don't have any idea to fix this. Can anyone please correct me to get correct time?
let dateString = "2020-08-11T05:32:33.000Z"
func approach1(){
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSXXXXX"
guard let date = dateFormatter.date(from: dateString) else {fatalError()}
printTime(date: date)
}
func approach2(){
let dateFormatter = ISO8601DateFormatter()
dateFormatter.formatOptions = [.withInternetDateTime, .withFractionalSeconds]
guard let date = dateFormatter.date(from: dateString) else { fatalError()}
printTime(date: date)
}
func printTime(date: Date){
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "h:mm a"
dateFormatter.amSymbol = "AM"
dateFormatter.pmSymbol = "PM"
let time = dateFormatter.string(from: date)
print("date: \(date)")
print("Time: \(time)") // Here it's always wrong time with those approach.
}
Current Result:
By default dateFormatter works with local time zone. To have correct display set its time zone to UTC : dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
There's nothing wrong. The date formatter returns the correct time
The date string
"2020-08-11T05:32:33.000Z"
represents a date in UTC(+0000).
Your local time zone is obviously UTC+0530. DateFormatter considers the local time zone by default.
To format the time also in UTC you have to set the time zone of the date formatter explicitly
func printTime(date: Date) {
let dateFormatter = DateFormatter()
dateFormatter.timeZone = TimeZone(identifier: "UTC")
dateFormatter.dateFormat = "h:mm a"
dateFormatter.amSymbol = "AM"
dateFormatter.pmSymbol = "PM"
let time = dateFormatter.string(from: date)
print("date: \(date)")
print("Time: \(time)")
}
You need to specify the time zone when formatting to string (by default it's using your current local time zone):
func printTime(date: Date) {
let dateFormatter = DateFormatter()
dateFormatter.timeZone = TimeZone(abbreviation: "UTC") // <- add here
dateFormatter.dateFormat = "h:mm a"
dateFormatter.amSymbol = "AM"
dateFormatter.pmSymbol = "PM"
let time = dateFormatter.string(from: date)
print("date: \(date)")
print("Time: \(time)")
}

IOS conversion of UTC to local time not accurate

I'm trying to convert a UTC to device local time in Swift 4. I found many solutions on stackoverflow for that and I implemented them in my code. My code works fine but it doesn't return the right answer.
My code is:
func UTCToLocal() -> String {
let a = "2:36:27 PM"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "hh:mm:ss aa"
dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
let dt = dateFormatter.date(from: a)
dateFormatter.timeZone = TimeZone.current
dateFormatter.defaultDate = Date()
dateFormatter.dateFormat = "hh:mm:ss aa"
return dateFormatter.string(from: dt!)
}
According to my local time, Lebanon, this method should return the value 5:36:27 PM. However, it is returning 4:36:27 PM.
Note: I checked my device's local time and it's set correctly
This is due to Daylight Saving Time in Lebanon. You can take the time offset into consideration by using the daylightSavingTimeOffset(for:) method on your time zone:
func UTCToLocal() -> String {
let a = "2:36:27 PM"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "hh:mm:ss aa"
dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
let dt = dateFormatter.date(from: a)
dateFormatter.timeZone = TimeZone.current
let offset = dateFormatter.timeZone.daylightSavingTimeOffset(for: dt!)
dateFormatter.defaultDate = Date()
dateFormatter.dateFormat = "hh:mm:ss aa"
return dateFormatter.string(from: dt! + offset)
}
UTCToLocal()
Note that dt is "Jan 1, 2000 at 2:36 PM" since you haven't specified the .day, .month, and .year date components in a

Time-zone Swift

Can somebody recommend how to get a current time/date in a specific timezone ?
This is what I got so far:
let date = NSDate()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm"
let str = dateFormatter.string(from: date as Date)
dateFormatter.timeZone = NSTimeZone(name: "UTC") as! TimeZone
print(str)
It gives me my current time, I'm not sure how to change timezone correctly so it would print time in a specified time-zone...

UTC Date To Current TimeZone String and Result StringDate to Current TimeZone Date

I get the current date in UTC and I converted it into Local TimeZone that result comes in string.
Now, I converted that Result String Date into a Date with the same Local TimeZone but it gives the date in UTC.
Below is the Code I am using:
print("current date :\( Date())") //current date : 2017-03-08 11:11:12 +0000
let dateFormatter = DateFormatter()
dateFormatter.timeZone = TimeZone.current
dateFormatter.dateFormat = "dd-MM-yyyy HH:mm:ss Z"
let localDate : String = dateFormatter.string(from: Date())
print("localDate date :\(localDate)") // localDate date: 08-03-2017 16:41:12 +0530
let dateFormatter1 = DateFormatter()
dateFormatter1.timeZone = TimeZone.current
dateFormatter1.dateFormat = "dd-MM-yyyy HH:mm:ss Z"
let dateCurrent = dateFormatter1.date(from: localDate)!
print("dateCurrent date :\(dateCurrent)") // dateCurrent date: 2017-03-08 11:11:12 +0000
dateCurrent
I want in
08-03-2017 16:41:12 +0530
only of type Date not of type String.
Where have I gone wrong?
Thanks everyone for your Answers,
I dug out and I found the solution. Here I am sharing the code.
let currentDate = Date()
let CurrentTimeZone: TimeZone = TimeZone(abbreviation: "UTC")!
let SystemTimeZone: TimeZone = TimeZone.current
let currentGMTOffset: Int = CurrentTimeZone.secondsFromGMT(for: currentDate)
let SystemGMTOffset: Int = SystemTimeZone.secondsFromGMT(for: currentDate)
let interval: TimeInterval = Double(SystemGMTOffset) - Double(currentGMTOffset)
let todayDate: Date = Date(timeInterval: interval, since: currentDate)
print(" Today Date : \(todayDate)")
This is how Date objects work. You simply keep the date object until you need to output it to the user or an API, at which point you can convert it to a string in the appropriate time zone.
A Date object essentially just wraps a unix timestamp with some helper methods for dealing with it.
try this, Hope it helps
let date: String = "dd-MM-yyyy HH:mm:ss Z"
var dateArr = date.components(separatedBy: " ")
let dateCurrent = (dateArr [0] )
print(dateCurrent)
Issue with your dateFormatter. Here is my code to conver UTC to Local and Local to UTC:
convertUTCtoLocal(strUTCDate: "09-03-2017 03:54:00")
convertLocalToUTC(strUTCDate: "09-03-2017 09:24:00")
Convert UTC to Local
func convertUTCtoLocal(strUTCDate: String){
let dateFormater : DateFormatter = DateFormatter();
dateFormater.timeZone = TimeZone(abbreviation: "UTC")
dateFormater.dateFormat = "dd-MM-yyyy HH:mm:ss"
let dateFromString = dateFormater.date(from: strUTCDate)
dateFormater.timeZone = NSTimeZone.local as TimeZone!
dateFormater.dateFormat = "dd-MM-yyyy HH:mm:ss"
let localData = dateFormater.string(from: dateFromString!)
print("Your local date is \(localData)")
}
Convert Local to UTC
func convertLocalToUTC(strUTCDate: String){
let dateFormater : DateFormatter = DateFormatter();
dateFormater.dateFormat = "dd-MM-yyyy HH:mm:ss"
dateFormater.timeZone = NSTimeZone.local as TimeZone!
let dateFromString = dateFormater.date(from: strUTCDate)
dateFormater.timeZone = NSTimeZone(abbreviation: "UTC") as TimeZone!;
dateFormater.dateFormat = "dd-MM-yyyy HH:mm:ss"
let localData = dateFormater.string(from: dateFromString!)
print("Your UTC date is \(localData)")
}

Resources