How can I get current GMT 0 in Swift 3? - ios

I'm looking for a way to display the current GMT-0 time.
So far, I've been doing it like:
let UTCDate = Date()
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let defaultTimeZoneStr = formatter.string(from: UTCDate)
However it returns the current time of the phone.
How can I get the current GMT-0 time using Swift 3?

Before the last line of your code, insert this line:
formatter.timeZone = TimeZone(secondsFromGMT:0)
Or this line:
formatter.timeZone = TimeZone(identifier:"GMT")

Related

Swift: Create a Date() from a Google Drive API Timestamp

I'm working with the Google Drive API in Swift, and one of the pieces of data I'm working with is a timestamp, formatted like this: 2021-04-18T22:50:33.235Z
I keep getting nil for my Date object, and I assume it has to do with my DateFormatter(), but I can't seem to figure out exactly what is wrong.
Here's my code that I tried:
var creationDateAsString = "2021-04-18T22:50:33.235Z"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
if let date = dateFormatter.date(from: creationDateAsString) {
//do what I need to do
}
Thanks for any help you can offer.
It's true that this code, for some reason, worked in the playground, but not in my actual Swift app. I was, however, able to solve the issue by defining the locale:
var creationDateAsString = "2021-04-18T22:50:33.235Z"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
if let date = dateFormatter.date(from: creationDateAsString) {
//do what I need to do
}

Wrong date in swift 5 after conversion [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)
Closed 3 years ago.
I am converting current date into GMT/UTC date string. But every time it returns me with wrong date.
My todays date is 07 February 2020, 11:09:20 AM. You can refer below image.
Here is my code :
let apiFormatter = DateFormatter()
//apiFormatter.dateStyle = DateFormatter.Style.long
//apiFormatter.timeStyle = DateFormatter.Style.long
//apiFormatter.calendar = Calendar.current
apiFormatter.timeZone = TimeZone.init(identifier: "GMT") //TimeZone(abbreviation: "UTC") //TimeZone.current //
//apiFormatter.locale = Locale.current
//apiFormatter.dateFormat = "yyyy-MM-DD HH:mm:ss"
apiFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
//apiFormatter.dateFormat = "yyyy-MM-dd'T'hh:mm:ssZ"
let endDate = apiFormatter.string(from: Date())
print(endDate)
And what I am getting in return is also you can check in image - 2020-02-38T05:33:34.598Z. I have tried with all the format, but no any luck. Can anyone suggest where it is going wrong?
First of all, the format should be:
apiFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
The Z is not a literal letter, it's the description of the time zone. However, making it a literal won't probably make a problem.
The 38 for day from your output is obviously caused by the DD format you have commented out.
Nevertheless, you have to set the locale:
apiFormatter.locale = Locale(identifier: "en_US_POSIX")
Otherwise you will have problems with 12/24h switching.
let apiFormatter = DateFormatter()
apiFormatter.locale = Locale(identifier: "en_US_POSIX")
// remove this if you want to keep your current timezone (shouldn't really matter, the time is the same)
apiFormatter.timeZone = TimeZone(secondsFromGMT: 0)
apiFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
let endDate = apiFormatter.string(from: Date())
print(endDate) // 2020-02-07T08:25:23.470+0000
print(Date()) // 2020-02-07 08:25:23 +0000
Also note that you can use ISO8601DateFormatter instead of DateFormatter.
Try this and adjust according to what format you are getting from server -
private func getFormatedDateInString(_ dateString: String) -> String? {
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
dateFormatter.timeZone = TimeZone(identifier: "UTC")
if let date = dateFormatter.date(from: dateString) {
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
dateFormatter.timeZone = TimeZone.current
let timeStamp = dateFormatter.string(from: date)
return timeStamp
}
return nil
}

wrong week days symbols (weekdaySymbols)

i am trying to change the weeks day symbols in swift
with the code below
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEEE d MMM"
dateFormatter.weekdaySymbols = ["Måndag", "Tisdag", "Onsdag", "Torsdag", "Fredag", "Lördag", "Söndag"]
let dateString = dateFormatter.string(from: Date())
print(dateString)
And i am getting this wrong day blow
Tisdag 9 Jul
but today is Monday
Instead of managing weekday names yourself, you could use DateFormatter's template api.
let dateFormatter = DateFormatter()
dateFormatter.setLocalizedDateFormatFromTemplate("dEEEEMMM")
let dateString = dateFormatter.string(from: Date()).capitalized(with: locale)
print(dateString)
This should use the phone's locale, which should be what user wants most times.
But you can also set the locale manually.
If you add
let locale = Locale(identifier: "sv_SE")
dateFormatter.locale = locale
it will print
Måndag 9 Juli
if you use
let locale = Locale(identifier: "de_DE")
it prints
Montag, 9. Juli
weekdaySymbols start with sunday, not with monday. You need to change this:
dateFormatter.weekdaySymbols = ["Söndag", "Måndag", "Tisdag", "Onsdag", "Torsdag", "Fredag", "Lördag"]

How to format a date using timezone in Swift?

I have checked other questions but none of them helped me much.
I have following string:
let dateString = "2018-04-29T21:00:00.000Z"
I have successfully converted it to date using the following:
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
let convertedDate = dateFormatter.date(from: dateString)
But now I only want the time "hh:mm a" using timezone such as "+8". I have tried following way but it's not working:
let dateFormatter = DateFormatter()
dateFormatter.timeZone = TimeZone(abbreviation: "UTC+8")
dateFormatter.dateFormat = "hh:mm a"
let requiredTime = dateFormatter.string(from: convertedDate!)
Can anyone help me to overcome this problem?
The format you want is hh:mm a Z, which will provide the +0800.
You want to create a TimeZone which +8 hours from GMT, normally I prefer to use the appropriate abrivations (ie AET), but I guess if you don't have that, you can create a TimeZone using secondsFromGMT, for example...
let tz = TimeZone(secondsFromGMT: 8 * 60 * 60)
let toFormatter = DateFormatter()
toFormatter.timeZone = tz
toFormatter.dateFormat = "hh:mm a Z"
let requiredTime = toFormatter.string(from: convertedDate!)
Which based on your example data, will produce a value of...
05:00 AM +0800

Parsing date string coming from Fitbit API

For the life of me I cannot figure out how to convert date string coming back from the Fitbit API, to an NSDate/Date object on iOS.
The date string is in the following format:
2017-01-21T10:39:43.000
The API I am using is documented here: https://dev.fitbit.com/docs/devices/, and each device comes back with a "lastSyncTime" property in the JSON.
I've tried several different date formats that keep returning a nil value for the date.
let formatter = DateFormatter()
// formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
// formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss'Z'"
// formatter.dateFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"
// formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssz"
// formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSz"
formatter.dateFormat = "yyyy-MM-dd"
let date = formatter.date(from: lastSyncTime)
I've tried all these different date formats to no avail.
Any help would be greatly appreciated here. Thanks.
Use this formatter to parse it.
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS"
formatter.date(from: dateString)
I hope this helps
This works for me:
import Foundation
let date = "2017-01-21T10:39:43.000"
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS"
let formatDate = formatter.date(from: date)
print(formatDate!)
Output: 2017-01-21 16:39:43 +0000

Resources