I am getting timeStamp from API as EST, if my device is in India or in USA then it should show (IST or CST) the time according to the timezone. How can I do it?
let currentTimeZone = formatter.timeZone.identifier
print(currentTimeZone) // Asia/Kolkata
How can I get EST, CST, EDT, etc. instead of getting Asia/Kolkata?
You can get the name from the name property:
NSLog(#"Name : %#", timeZone.name);
Output will be :
2015-07-10 13:31:46.292 APP[2904:50429] Name : Asia/Calcutta
Then You can to get the name with the desired style:
NSTimeZone* timeZone = [NSTimeZone timeZoneWithAbbreviation:#"IST"];
NSString* timeZoneName = [timeZone localizedName:NSTimeZoneNameStyleStandard locale:[NSLocale currentLocale]];
NSLog(#"Name : %#", timeZoneName);
NSLog(#"%#",[NSTimeZone abbreviationDictionary]);
output will be:
2015-07-10 11:44:05.954 APP[1472:26120] {
ADT = "America/Halifax";
AKDT = "America/Juneau";
AKST = "America/Juneau";
ART = "America/Argentina/Buenos_Aires";
AST = "America/Halifax";
BDT = "Asia/Dhaka";
BRST = "America/Sao_Paulo";
BRT = "America/Sao_Paulo";
BST = "Europe/London";
CAT = "Africa/Harare";
CDT = "America/Chicago";
CEST = "Europe/Paris";
CET = "Europe/Paris";
CLST = "America/Santiago";
CLT = "America/Santiago";
COT = "America/Bogota";
CST = "America/Chicago";
EAT = "Africa/Addis_Ababa";
EDT = "America/New_York";
EEST = "Europe/Istanbul";
EET = "Europe/Istanbul";
EST = "America/New_York";
GMT = GMT;
GST = "Asia/Dubai";
HKT = "Asia/Hong_Kong";
HST = "Pacific/Honolulu";
ICT = "Asia/Bangkok";
IRST = "Asia/Tehran";
IST = "Asia/Calcutta";
JST = "Asia/Tokyo";
KST = "Asia/Seoul";
MDT = "America/Denver";
MSD = "Europe/Moscow";
MSK = "Europe/Moscow";
MST = "America/Denver";
NZDT = "Pacific/Auckland";
NZST = "Pacific/Auckland";
PDT = "America/Los_Angeles";
PET = "America/Lima";
PHT = "Asia/Manila";
PKT = "Asia/Karachi";
PST = "America/Los_Angeles";
SGT = "Asia/Singapore";
UTC = UTC;
WAT = "Africa/Lagos";
WEST = "Europe/Lisbon";
WET = "Europe/Lisbon";
WIT = "Asia/Jakarta";
}
then you can find what is your current timezone.
let timeStr = "15/02/2014 01:00:00"
let currentTimezone = TimeZone.current
let targetTimezone = TimeZone(abbreviation: "EST") ?? TimeZone.current
let dateFormatter1 = DateFormatter()
dateFormatter1.dateFormat = "dd/MM/yyyy HH:mm:ss"
let localOffset = currentTimezone.secondsFromGMT()
let targetOffset = targetTimezone.secondsFromGMT()
let localDate = dateFormatter1.date(from: timeStr)
let targetDate = localDate?.addingTimeInterval(TimeInterval(targetOffset - localOffset))
Try this. TimeZone.current will return you the current timezone used by system.
try this code:
let timeZone = TimeZone.current
let name = timeZone.localizedName(for: .shortStandard, locale: Locale.current)
print("Name :: ", name)
Please try this.It works for me.
Please get the time from server from UTC format.
then :
func convertFromUTCtoLocal(dateToConvert:String) -> String {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssz"
let convertedDate = formatter.date(from: dateToConvert)
formatter.timeZone = TimeZone(abbreviation: "EST")
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
return formatter.string(from: convertedDate!)
}
Set the timezone according to your requirement.It may helps to you. Thank you
ObjC:
NSTimeZone *timeZoneLocal = [NSTimeZone localTimeZone];
NSString *strTimeZoneAbb = [timeZoneLocal abbreviation];
NSString *strTimeZoneName = [timeZoneLocal name];
NSLog(#"%#",strTimeZoneAbb); //Ex:Output:GMT +5:10
NSLog(#"%#",strTimeZoneName); //Ex:Output:Asia/Calcutta
Swift:
let timezone = NSTimeZone.local
print(timezone.abbreviation)
print(timezone.name)
Related
I want to convert this String to Date
Mon, 03 May 2021 00:00:00 GMT
let dateString = "Mon, 03 May 2021 00:00:00 GMT"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEE, dd MMM yyyy hh:mm:ss 'Z'"
date = dateFormatter.date(from: dateString)
but it doesn't work, date is nil. So what is the correct format for this case?
use the date format as like EEE, dd MMM yyyy hh:mm:ss Z
let dateString = "Mon, 03 May 2021 00:00:00 GMT"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEE, dd MMM yyyy hh:mm:ss Z"
let date = dateFormatter.date(from: dateString)
print(date)
You need to use HH since this is 24h format and since the day and month names are in English you also need to set the locale for the formatter so it doesn't use the users current locale
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "EEE, dd MMM yyyy HH:mm:ss Z"
I have variables that I want to use to create a string that can be printed to a UILabel.
maturityDate | ComparisonTerm | monthsToMaturity | ComparisonRate
The following are variables:
maturitDate:Date // value (value to return MMM DD, YYYY) Ex June 23, 2017
ComparisonTerm: Double
MonthsToMaturity: Double
ComparisonRate: Double
I want the "|" bars to be separators. My biggest challenge has been dealing with the date value. It currently returns 2021-09-01 04:00:00 + 000 as an example.
It should be pretty simple for the other three variable like this
"\(ComparisonTerm) | \(monthsToMaturity) | \(ComparisonRate)"
For the date, you have to use a date formatter
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .mediumStyle // or .shortStyle
dateFormatter.timeStyle = .noStyle
So the required date String is basically
"\(dateFormatter.dateFromString(maturitDate)) | \(ComparisonTerm) | \(monthsToMaturity) | \(ComparisonRate) "
let dateFormatterPrint = DateFormatter()
dateFormatterPrint.dateFormat = "MMM dd,yyyy"
let maturityDate = Date()
let maturityDateStr = dateFormatterPrint.string(from: maturityDate)
let comparisonTerm : Double = 0.1
let monthsToMaturity : Double = 0.2
let comparisonRate : Double = 0.3
let str = "\(maturityDateStr) | \(comparisonTerm) | \(monthsToMaturity) | \(comparisonRate)"
I'm using NSDateFormater to format dates that i get from my server.
All the formatting works correctly except of AM/PM
let f = "EEEE, MMM dd, hh:mma"
dateFormatter.dateFormat = f
let d = dateFormatter.stringFromDate(NSDate())
print("formated date: \(d)")
i get:
formated date: Tuesday, Feb 23, 11:30
and it should be:
formated date: Tuesday, Feb 23, 11:30am
Am i missing something?
Solution:
My IPhones time is set to 24 hour format, i changed it to 12 and it also changed in my app.
I think it should be
let f = "EEEE, MMM dd, hh:mm a"
I just tested this and it works
let dateFormatter = NSDateFormatter()
let f = "EEEE, MMM dd, hh:mm a"
dateFormatter.dateFormat = f
let d = dateFormatter.stringFromDate(NSDate())
print("formated date: \(d)")
Demo here
This is the timestamp I am trying to get my app to read: Thu, 03 Mar 2016 16:06:42 GMT
This is the closest I have, but haven't had any success:
[df setDateFormat:#"EEE, dd MMM YYYY HH:mm:ss z"];
EDIT: My current format prints-to/reads: Tue, 02 M02 2016 11:36:57 GMT-5
is there anything I'm missing?
Final Edit:
I should have just posted all my code...
[df setLocale:[NSLocale systemLocale]];
This was messing things up for me.
I believe your year is incorrect. Change your dateformat to
[df setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss z"];
See documentation on formatter dates here
I tried to use your date format and convert it to a nsdate and back to a string. It should work with the format string you used.
let time = "Thu, 03 Mar 2016 16:06:42 GMT"
let dateformat = "EEE, dd MMM yyyy HH:mm:ss z"
var dateformatter = NSDateFormatter()
dateformatter.dateFormat = dateformat
dateformatter.timeZone = NSTimeZone(forSecondsFromGMT: 0)
var date = dateformatter.dateFromString(time)
var newTime = dateformatter.stringFromDate(date!)
// newTime = "Thu, 03 Mar 2016 16:06:42 GMT"
Edit: I changed the dateformat to use lowercase ys for the year as suggested by Devster101. This is the correct format according to this spezification which is used since iOS 7
I am developing an iOS app with twitter and am encountering a problem when retrieving the date in the format I want. When I phase the the JSON I come out with each tweet and the post date of them however it is in a different time zone. What converter can I use that will take ex: "time_zone" = "Pacific Time (US & Canada)" and convert the time it into the localized time. I already go it to convert the date string into an NSDate so I just don't know how to apply a timezone change. Thank You.
You could use timezone attribute of NSDateFormatter.
Just to give you an example, if you want the date to be in EST, you use:
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#\"EST\"]];
Here's the full list of supported abbreviations using NSTimeZone.
ADT = "America/Halifax";
AKDT = "America/Juneau";
AKST = "America/Juneau";
ART = "America/Argentina/Buenos_Aires";
AST = "America/Halifax";
BDT = "Asia/Dhaka";
BRST = "America/Sao_Paulo";
BRT = "America/Sao_Paulo";
BST = "Europe/London";
CAT = "Africa/Harare";
CDT = "America/Chicago";
CEST = "Europe/Paris";
CET = "Europe/Paris";
CLST = "America/Santiago";
CLT = "America/Santiago";
COT = "America/Bogota";
CST = "America/Chicago";
EAT = "Africa/Addis_Ababa";
EDT = "America/New_York";
EEST = "Europe/Istanbul";
EET = "Europe/Istanbul";
EST = "America/New_York";
GMT = GMT;
GST = "Asia/Dubai";
HKT = "Asia/Hong_Kong";
HST = "Pacific/Honolulu";
ICT = "Asia/Bangkok";
IRST = "Asia/Tehran";
IST = "Asia/Calcutta";
JST = "Asia/Tokyo";
KST = "Asia/Seoul";
MDT = "America/Denver";
MSD = "Europe/Moscow";
MSK = "Europe/Moscow";
MST = "America/Denver";
NZDT = "Pacific/Auckland";
NZST = "Pacific/Auckland";
PDT = "America/Los_Angeles";
PET = "America/Lima";
PHT = "Asia/Manila";
PKT = "Asia/Karachi";
PST = "America/Los_Angeles";
SGT = "Asia/Singapore";
UTC = UTC;
WAT = "Africa/Lagos";
WEST = "Europe/Lisbon";
WET = "Europe/Lisbon";
WIT = "Asia/Jakarta";