NSDate Change Based on Time Zone String - ios

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

Related

How to check my device time is in which timezone?

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)

Swift - Timezone off by one hour / secondsFromGMT incorrect

This should be a really simple question but I just can't seem to wrap my head around it.
Given my timezone is EDT (GMT-4), why does 04:00 in GMT turn into 23:00 and not 00:00?
// The offset is -4 hours
let offsetFromGMT = Calendar.current.timeZone.secondsFromGMT() / 60 / 60
// 2017-03-12 04:00
var destinationComponents = DateComponents()
destinationComponents.timeZone = TimeZone(secondsFromGMT: 0)
destinationComponents.year = 2017
destinationComponents.month = 03
destinationComponents.day = 12
destinationComponents.hour = -offsetFromGMT // 4 hours
// Why is this 2017-03-11 23:00 and not 2017-03-12 00:00?
let date = Calendar.current.date(from: destinationComponents)!
// Outputs 23
Calendar.current.dateComponents([.hour], from: date).hour
Calendar.current.timeZone.secondsFromGMT()
is the current GMT offset for your time zone. In your case that
is 4 hours, because the current time zone in New York is EDT = GMT-4,
with daylight saving time active.
So your destinationComponents and date are four o'clock in
the morning Greenwich time:
2017-03-12 04:00:00 +0000
At that point, the time zone in New York was EST = GMT-5, and
daylight saving time not active. Therefore that date is 2017-03-11 23:00 in your local time zone.
I would proceed differently, avoiding "secondsFromGMT".
Example: "2017-03-12 00:00:00" New York time is "2017-03-12 05:00:00" GMT.
var srcComponents = DateComponents()
srcComponents.timeZone = TimeZone(identifier: "America/New_York")!
srcComponents.year = 2017
srcComponents.month = 3
srcComponents.day = 12
srcComponents.hour = 0
srcComponents.minute = 0
let date = Calendar.current.date(from: srcComponents)!
print(date) // 2017-03-12 05:00:00 +0000

Transforming date + time in a TimeStamp Object

I'm having some trouble with making a timestamp from a date and a time
What i'm trying to do:
date = "2016 2 21"
time = "03:00 UTC"
output = "Thu, 21 Feb 2016 03:00:00 UTC +00:00"
I'm getting the date from a form_for:
f.date_field(:date_first)
But I'm not sure of how should I pick up the time.
To give you a headstart:
dt = "2016-2-21"
time = "03:00 UTC"
dtime = DateTime.parse(dt + 'T' + time)
output = dtime.rfc2822
and the result is:
#=> "Sun, 21 Feb 2016 03:00:00 +0000"
Maybe you could do something like this
strftime("%d.%m.%Y. %H:%M:%S")
and also get your locale yml file from here
https://github.com/svenfuchs/rails-i18n/tree/master/rails/locale

NSDate, Can't get proper date format

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

How to covert the date string range into datatime data type

I need to convert the following raw string (date range) into ruby datetime datetype.
How to finish it on Rails ?
raw string
"2014 April/July 24-1"
convert to ruby datetime variable
start_date = 2014-04-24
end_date = 2014-07-01
raw string
"2015 April 06-20"
convert to ruby datetime variable
start_date = 2015-04-06
end_date = 2015-04-20
This may help
# In order to generate
# year = 2014
# months = "April/July"
# days = "24-1"
/(?<year>\d{4})\s*(?<months>\w+\/\w+)\s*(?<days>\d{1,2}\-\d{1,2})/ =~ "2014 April/July 24-1"
date1 = "#{year} #{months.split('/')[0]} #{days.split('-')[0]}"
date2 = "#{year} #{months.split('/')[1]} #{days.split('-')[1]}"
start_date = Date.strptime(date1, "%Y %b %d") #=> Thu, 24 Apr 2014
end_date = Date.strptime(date2, "%Y %b %d") #=> Tue, 01 Jul 2014

Resources