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...
Related
The date I'm having is this..
2021-03-05T12:21:18
But I want it in dd-MM-yyyy format. This is what I did for that...
dateFormatter.dateFormat = "YYYY-MM-dd'T'HH:mm:ss"
let date2 = dateFormatter.date(from: self.Due_Date) //self.Due_Date is 2021-03-05T12:21:18
dateFormatter.dateFormat = "dd-MM-yyyy"
self.Due_Date = dateFormatter.string(from: date2!) //CRASH HERE
But I get date2 as nil. So the code crashes at self.Due_Date = dateFormatter.string(from: date2!)
What could be the reason for this...?
The “yyyy” represents the calendar year of the date while the “YYYY” represents the year of the week.
for detail information, please see this Difference between 'YYYY' and 'yyyy' in NSDateFormatter
let getInputString = "2021-03-05T12:21:18"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
if let getInputDate = dateFormatter.date(from:getInputString){
dateFormatter.dateFormat = "dd-MM-yyyy"
let getoutputString = dateFormatter.string(from: getInputDate)
}
Note : as per #LeoDabus guidelines please ensure the self.Due_Date is empty or not before start the conversion.
func getDateWithoutTimeForDisplay() -> String {
let formatter = DateFormatter()
// formatter.locale = Locale(identifier: "en_US")
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
formatter.timeZone = TimeZone(identifier: "UTC")
formatter.locale = Locale(identifier: "en_US_POSIX")
let myStringafd = formatter.date(from: self)
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MM-dd-yyyy"
dateFormatter.timeZone = NSTimeZone.local
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
if let myStringafd = myStringafd {
let somedateString = dateFormatter.string(from: myStringafd)
return somedateString
}
return ""
}
this is my code to convert given UTC date to system date based on the local time zone.
The actual problem is, suppose the case "i have the time 1.00 AM on the date 25-08-2020, according to the Indian time zone, ( UTC time is 5.30 lesser than Indian time) the corresponding UTC date is 24-08-2020 due to the time difference with the Indian time". In this case i want to convert the UTC date(Because it is effecting the date to be display for the user) to the current system date.
My system date&time is 25-08-2020 1:00 AM (Indian time)
the corresponding UTC date is 24-08-2020. -5:30 hr
i need to convert the UTC date to the current local time
Try this out:
let currentDate = self.UTCToLocal(date: Date().description) //This will pass system date in UTC format to the function and function will return desired output in local timezone
func UTCToLocal(date: String) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ssz" //The date format should be exact as same as UTC date
dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
let dt = dateFormatter.date(from: date)
dateFormatter.timeZone = NSTimeZone.local
dateFormatter.dateFormat = "dd-MM-yyyy HH:mm:ss" //The desired date format in which you may want to return the value
return dateFormatter.string(from: dt!)
}
or you can check this post - Returning nil when converting string to date in swift and this link for dateFormatters - https://nsdateformatter.com/
I'm trying to convert the current time and date to a timestamp format. Since the service is receiving this format for timestamps:
2018-26-11T11:38:00Z
I decided to use a format like this:
yyyy-M-dd'T'H:mm:ss'Z'
But, when I use a date formatter to convert it, I'm getting an unwanted AM/PM tag at the end by default:
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-M-dd'T'H:mm:ss'Z'"
let currentTimeAndDate = Date()
let timeStamp = dateFormatter.string(from: currentTimeAndDate)
// Prints "2018-12-05T12:58:38 PMZ"
How can I remove that AM/PM by default at the end of the string?
Set the locale to fixed en_US_POSIX"
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
Or use the dedicated ISO8601 formatter
let dateFormatter = ISO8601DateFormatter()
let currentTimeAndDate = Date()
let timeStamp = dateFormatter.string(from: currentTimeAndDate)
Use yyyy-MM-dd'T'HH:mm:ssX for the format.
Set the formatter's timeZone to TimeZone(secondsFromGMT: 0).
Set the locale to the special "en_US_POSIX" locale: Locale(identifier: "en_US_POSIX")
Updated code:
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssX"
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
let currentTimeAndDate = Date()
let timeStamp = dateFormatter.string(from: currentTimeAndDate)
Or avoid all of that and use ISO8601DateFormatter.
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.
I am getting date string from server like that "2017-08-05T00:30:00.000+02:00". Below is my code for date formatter.
let dateFormatter: DateFormatter = DateFormatter()
let serverDateString = "2017-08-05T00:30:00.000+02:00"
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZZ"
dateFormatter.locale = Foundation.Locale(identifier: "en_US_POSIX")
let currentDate: Date = dateFormatter.date(from: serverDateString)!
But it returns 2017-08-04 22:30:00 +0000. Which is wrong. Any suggestions?
In Swift 3
You can change your server time string to UTC time Date as:
let dateFormatter: DateFormatter = DateFormatter()
var serverDateString = "2017-08-05T00:30:00.000+02:00"
let index = serverDateString.index(serverDateString.startIndex, offsetBy: 19)
serverDateString = serverDateString.substring(to: index)
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
dateFormatter.timeZone = TimeZone.init(identifier: "UTC")
let currentDate: Date = dateFormatter.date(from: serverDateString)!
Your Code for parsing date is correct: 2017-08-05T00:30:00.000+02:00 and 2017-08-04 22:30:00 +0000 represent the same time, just in different time zones. Your only problem is that Date doesn't actually store the time zone
yyyy-MM-dd'T'hh:mm:ss.SSSZZZZZZ
This is correct date formate for your input string. Here is the Apple Document for more description.
Here is my code:
var strInputDateString: String = "2017-08-05T00:30:00.000+02:00"
var dateFormat = DateFormatter()
dateFormat.dateFormat = "yyyy-MM-dd'T'hh:mm:ss.SSSZZZZZZ"
//Set new dateFormate
var date1: Date? = dateFormat.date(from: strInputDateString)
dateFormat.dateFormat = "dd-MM-YYYY hh:mm:ss"
// Your Desire
var strOutputDateString: String = dateFormat.string(from: date1!)
print("\(strInputDateString)")
print("\(strOutputDateString)")
output:
Main input String:2017-08-05T00:30:00.000+02:00
Converted String: 05-08-2017 04:00:00
For now, I tried to convert your date format with my custom. It's give me the perfect output.
Actually, your code is good.
I copied your data and ran it in playground.
What kind of format you want it be?
Try this
let dateFormatter: DateFormatter = DateFormatter()
let serverDateString = "2017-08-05T00:30:00.000+02:00"
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssXXX"
dateFormatter.locale = Foundation.Locale(identifier: "en_US_POSIX")
let currentDate: Date = dateFormatter.date(from: serverDateString)!
Hope this helps
The step you are missing is to again use your DateFormatter to format the date into a string. When you use print on the date, it will always show GMT. For illustration purposes, I've set the TimeZone to Berlin to match the original offset of +2:00.
let dateFormatter: DateFormatter = DateFormatter()
let serverDateString = "2017-08-05T00:30:00.000+02:00"
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZZ"
// Hard set to Berlin
dateFormatter.timeZone = TimeZone(identifier: "Europe/Berlin")
let currentDate: Date = dateFormatter.date(from: serverDateString)!
// Your missing step
dateFormatter.string(from: currentDate)
Remember that DateFormatters are about:
Parsing from a String - df.date(from: String)
Formatting from a Date - df.string(from: Date)