iOS Date timezone conversion not working - ios

In my playground, I'm testing conversion of date time but it doesn't seem to be converting:
import UIKit
let dateString = "2017-12-01 10:00:00 +0000"
print("original date = \(dateString)")
var formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss Z"
formatter.timeZone = TimeZone(identifier: "Asia/Shanghai")
if let date = formatter.date(from: dateString) {
print("converted date = \(date)")
}
print("test")
The output is:
original date = 2017-12-01 10:00:00 +0000
converted date = 2017-12-01 10:00:00 +0000
test
I'm expecting the converted date to say something like 2017-12-01 18:00:00 +0800 (since Shanghai is 8 hours ahead of UTC 00:00:00)

Please try this:
let dateString = "2017-12-01 10:00:00 +0000"
print("original date = \(dateString)")
var formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss Z"
formatter.timeZone = TimeZone.init(abbreviation: "UTC")
if let date = formatter.date(from: dateString) {
formatter.timeZone = TimeZone(identifier: "Asia/Shanghai")
let localTime = formatter.string(from: date)
print(localTime)
}
print("test")
Output:
original date = 2017-12-01 10:00:00 +0000
2017-12-01 18:00:00 +0800
test

GMT:
formatter.timeZone = TimeZone(abbreviation: NSTimeZone.local.abbreviation()!)
UTC:
formatter.timeZone = TimeZone(abbreviation: NSTimeZone.default.abbreviation()!)
To display local datetime, i use the GMTTimeZone.

Related

Swift convert datetime format to another format

My scenario, I am trying to converting Oct 31, 2019 5:20 PM (MMM dd, yyyy) to 2019-10-31 05:20:00 (yyyy-MM-dd h:mm:ss)
String DateTime: Oct 29, 2019 11:00 PM
Need to convert: "yyyy-MM-dd h:mm:ss"
let formatter = DateFormatter() // Converting 2019-10-29 to Oct 29, 2019
formatter.dateFormat = "yyyy-MM-dd"
let yourDate = formatter.date(from: fullDate)
formatter.dateFormat = "yyyy-MM-dd h:mm:ss"
let myStringafd = formatter.string(from: yourDate!)
print(myStringafd)
It crashes because your datestring format and the format in which you want to convert is different. Change the dateFormat to dateFormatter.dateFormat = "yyyy-MM-dd hh:mm a"
Here is the code:-
let dateString = "2020-05-01 11:00 PM"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd hh:mm a"
dateFormatter.locale = Locale.init(identifier: "en_GB")
let dateres = dateFormatter.date(from: dateString)
print(dateres)
You have to first create date from the available date string. And then convert it back to string with the required dateFormat as below,
let dateString = "Oct 29, 2019 11:00 PM" // Or "Oct 31, 2019 5:20 PM"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMM dd, yyyy hh:mm a"
dateFormatter.locale = Locale(identifier: "en_GB")
let stringTodate = dateFormatter.date(from: dateString)
print(stringTodate!.description(with: dateFormatter.locale)) //Tuesday, 29 October 2019 at 23:00:00
dateFormatter.dateFormat = "yyyy-MM-dd hh:mm:ss"
let finalDate = dateFormatter.string(from: stringTodate!)
print(finalDate) // 2019-10-29 11:00:00
dateFormatter.dateFormat = "yyyy-MM-dd hh:mm a"
let finalDate1 = dateFormatter.string(from: stringTodate!)
print(finalDate1) // 2019-10-29 11:00 pm

Incorrect DateFormatter result when using timeIntervalSince1970

When I run the following code
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MM/dd/yyyy HH:mm:ss"
print(dateFormatter.string(from: Date(timeIntervalSince1970: 0)))
This gets printed out:
01/01/1970 08:00:00
which is supposed to be:
01/01/1970 00:00:00
what could be wrong?
Date(timeIntervalSince1970:) creates a date relative to 00:00:00 UTC on 1 January 1970 by the given number of seconds.
DateFormatter by default formats the date according to your local timezone.
Apparently you are in the GMT+8 timezone, so "00:00:00 UTC" is
"08:00:00" on your clock.
You can assign a different timezone to the date formatter if
required. To print the formatted date relative to UTC use
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MM/dd/yyyy HH:mm:ss"
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0) // <--- HERE
print(dateFormatter.string(from: Date(timeIntervalSince1970: 0)))
Try this and you will get the date according to your TimeZone :
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MM/dd/yyyy HH:mm:ss"
let timeZoneDifference = TimeInterval(TimeZone.current.secondsFromGMT())
let date = dateFormatter.string(from: Date(timeIntervalSince1970: 0))
print(date)
let dateAccoringToMyTimeZone = dateFormatter.string(from:
Date(timeIntervalSince1970: timeZoneDifference))
print(dateAccoringToMyTimeZone)

Getting date from datepicker wrt different timezones

I'm using a standard DatePicker and printing the date on top of it.
For some reason my Date Picker has it's timezone set to IST, which maybe because of the locale.
Let's get into an example:
DatePicker -> 12:00 PM Date reads -> 06:30:00 +0000
Which is correct as the date will print UTC time and IST is UTC+5:30
Now when I change the timezone of the DatePicker, let's say to London which is GMT+1,
DatePicker -> 12:00 PM Date reads -> 06:30:00 +0000
Where in the date should actually read 11:00:00
How I am changing the DatePicker's timezone:
if let tmz = selectTimeZone {
datePicker.timeZone = tmz
}
i have the same issue sometime before but for the another reason. Try this hope it will help.
First -
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
var startDate = dateFormatter.string(from: yourdatepicker.date)
startDate = localToUTC(date: startDate)
Second we convert this to local to UTC , here we mention the TimeZone to system time
func localToUTC(date:String) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
dateFormatter.timeZone = NSTimeZone.system
dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let dt = dateFormatter.date(from: date)
return dateFormatter.string(from: dt!)
}
Third if you want to conver UTC to your Local time
func UTCToLocal(date:String) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
let dt = dateFormatter.date(from: date)
dateFormatter.timeZone = NSTimeZone.system
dateFormatter.dateFormat = "hh:mm a"
return dateFormatter.string(from: dt!)
}

Convert string to date with timezone

i have date string 2017-01-03T10:45:00.000+02:00 and I need to transfer it to something like this 2017-01-03 10:45:00 +0200
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
// this formatter returning 2017-01-03 08:45:00 +0000 but I need
// 2017-01-03 10:45:00 +0200
Thank you for your help!
Try this,
Swift 3.0
let dateStr = "2017-01-03T10:45:00.000+02:00"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd\'T\'HH:mm:ss\'.000\'z"
dateFormatter.timeZone = NSTimeZone(name: "UTC") as TimeZone!
let date = dateFormatter.date(from: dateStr)
print("Date from String : \(date)")
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss xxxx'"
dateFormatter.timeZone = NSTimeZone(forSecondsFromGMT: 7200) as TimeZone!
print("String From Date : \(dateFormatter.string(from: date!))")
Result:
Date from String : Optional(2017-01-03 08:45:00 +0000)
String From Date : 2017-01-03 10:45:00 +0200
This one works for me, Hope it help's you
let dateString = "2017-01-03 08:45:00 +0000"
// create dateFormatter with UTC time format
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss xxxx"
dateFormatter.timeZone = NSTimeZone(name: "UTC") as TimeZone!
let date = dateFormatter.date(from: dateString)// create date from string
print("Date1 : \(date)")
// change to a readable time format and change to local time zone
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss xxxx'"
dateFormatter.timeZone = NSTimeZone(forSecondsFromGMT: 7200) as TimeZone!
print("Date2 : \(dateFormatter.string(from: date!))")
If you want to convert String to Date try the below code and let me know,
let dateString = "2016-10-25"
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let FormatDate = dateFormatter.dateFromString(dateString)
print(FormatDate)
More over you can get the time from below format:
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
Try this:
let dateString = "2017-01-03T10:45:00.000+02:00"
let dateFormatter = DateFormatter()
dateFormatter.timeZone = NSTimeZone(name: "UTC") as TimeZone!
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss SSSZ"
let FormatDate = dateFormatter.dateFromString(dateString)
print(FormatDate)

How to change string to date without converting to any timezone in Swift?

I know there are lot of questions passing around over this simple issue, but still I couldn't get a clear idea.
Here is what I want:
SelectedDateString = "19-08-2015 09:00 AM"
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy HH:mm a"
dateFormatter.timeZone = NSTimeZone();
let SelectedUTCDate = dateFormatter.dateFromString(SelectedDateString)!
println("SelectedLocalDate = \(SelectedLocalDate)")
// OUTPUT: SelectedLocalDate = 2015-08-18 18:30:00 +0000
If I dont use TimeZone:
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy HH:mm a"
// dateFormatter.timeZone = NSTimeZone();
let SelectedUTCDate = dateFormatter.dateFromString(SelectedDateString)!
println("SelectedLocalDate = \(SelectedLocalDate)")
//OUTPUT: SelectedLocalDate = 2015-08-18 19:26:00 +0000
Why is there change in the time and Date? What I want is:
//OUTPUT: SelectedLocalDate = 2015-08-19 09:00:00 +0000
Also I want to convert the Local Date to Exact UTC Date
//Like this: SelectedUTCDate = 2015-08-19 03:30:00
inputString = "19-08-2015 10:45 am" // My localtime is 10:35 AM, so I set 10 mins from now
var currentUTCTime = NSDate() // currentUTCTime is 05: 15 AM.
I want to convert the inputString to its respective UTC Time and find the difference between the two times in both date and string.
//Like this Date: diffInDate: 00-00-0000 00:10 and
// Like this String: diffInString: 10 mins
How can I get both of these?
let dateString = "19-08-2015 09:00 AM"
let dateFormatter = DateFormatter()
dateFormatter.calendar = Calendar(identifier: .iso8601)
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
dateFormatter.dateFormat = "dd-MM-yyyy hh:mm a"
if let dateFromString = dateFormatter.date(from: dateString) {
print(dateFromString) // "2015-08-19 09:00:00 +0000"
dateFormatter.timeZone = .current
dateFormatter.dateFormat = "dd-MM-yyyy hh:mm a Z"
dateFormatter.string(from: dateFromString) // 19-08-2015 06:00 AM -0300"
}
extension Date {
func originToString(dateFormat: String) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = dateFormat
dateFormatter.calendar = Calendar(identifier: .iso8601)
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
return dateFormatter.string(from: self)
}
}

Resources