Convert Nsdate to String with a Specific Timezone in Swift

Convert NSDate to String with a specific timezone in SWIFT

Xcode 8 beta • Swift 3.0

Your ISO8601 date format is basic hms without Z. You need to use "xxxx" for "+0000".

"+0000" means UTC time.

let dateString = "2015-09-04 22:15:54 +0000"

let dateFormatter = DateFormatter()
dateFormatter.calendar = Calendar(calendarIdentifier: .ISO8601)
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss xxxx"
dateFormatter.locale = Locale(localeIdentifier: "en_US_POSIX")
if let dateToBeSaved = dateFormatter.date(from: dateString) {
print(dateToBeSaved) // "2015-09-04 22:15:54 +0000"
}

If you need some reference to create your date format you can use this:

Sample Image

Converting date between timezones swift

Couldn't you just use your data formatter again with a different time zone and convert it? Such as

dateFormatter.timeZone = NSTimeZone(abbreviation: "GMT")
let gmtDate = dateFormatter.dateFromString(string: "your old date as string here")

Convert NSDate to String in iOS Swift

you get the detail information from Apple Dateformatter Document.If you want to set the dateformat for your dateString, see this link , the detail dateformat you can get here
for e.g , do like

let formatter = DateFormatter()
// initially set the format based on your datepicker date / server String
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"

let myString = formatter.string(from: Date()) // string purpose I add here
// convert your string to date
let yourDate = formatter.date(from: myString)
//then again set the date format whhich type of output you need
formatter.dateFormat = "dd-MMM-yyyy"
// again convert your date to string
let myStringDate = formatter.string(from: yourDate!)

print(myStringDate)

you get the output as

Sample Image

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

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"
}

How to convert 2 Strings (date and time) into one NSDate object in Eastern Time Zone in Swift

I don't think there's anything wrong with the time. EST is -04:00 so 16:30 EST = 20:30 GMT. And you wrote way more code that needed to be:

let scheduledServiceDateStr = "April 21, 2017"
let scheduledServiceTimeStr = "04:30 PM"

let formatter = DateFormatter()
formatter.dateFormat = "MMM dd, yyyy hh:mm a"
formatter.timeZone = TimeZone(identifier: "EST")

if let date = formatter.date(from: scheduledServiceDateStr + " " + scheduledServiceTimeStr) {
print(date)
}

NSDate set timezone in swift

how can i return a NSDate in a predefined time zone?

You can't.

An instance of NSDate does not carry any information about timezone or calendar. It just simply identifies one point in universal time.

You can interpret this NSDate object in whatever calendar you want. Swift's string interpolation (the last line of your example code) uses an NSDateFormatter that uses UTC (that's the "+0000" in the output).

If you want the NSDate's value as a string in the current user's calendar you have to explicitly set up a date formatter for that.

convert a string with locale date time to global time and back

Just a note:

HH means 24-hour format. Since you specify AM, you're using 12-hour format, so you should instead use hh in the formatter.

Change

dateFormatter.dateFormat = "dd/MM/yy HH:mm aa"

with

dateFormatter.dateFormat = "dd/MM/yy hh:mm aa"


Related Topics



Leave a reply



Submit