I'm new in iOS(Objective-c) coding and I'm stuck at timestamp.
I'm getting timestamp while JSON parsing ie.2017-04-30T14:30+00:00(GMT)
. How to get date, hour, minute and second from this timestamp?? I'm getting this format in GMT
so, is it possible to convert it into "IST"
? How?
Asked
Active
Viewed 2,027 times
0

dmaulikr
- 458
- 5
- 20
-
You want to change the locale I think this should help: http://stackoverflow.com/a/7259553/6203030 http://stackoverflow.com/questions/13138957/convert-date-in-mm-dd-yyyy-format-in-xcode – Aitor Pagán Apr 05 '17 at 11:31
-
Thank you @AitorPagán for suggestion. That helped in converting timezone. – dmaulikr Apr 05 '17 at 11:39
-
Bookmark this: http://nsdateformatter.com – diatrevolo Apr 05 '17 at 14:16
1 Answers
2
Date Format Patterns
A date pattern is a string of characters, where specific strings of characters are replaced with date and time data from a calendar when formatting or used to generate data for a calendar when parsing. The following are the characters used in patterns to show the appropriate formats for a given locale. The following are examples:
- (NSString *)curentDateStringFromDate:(NSDate *)dateTimeInLine withFormat:(NSString *)dateFormat {
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:dateFormat];
NSString *convertedString = [formatter stringFromDate:dateTimeInLine];
return convertedString;
}
Use it like below:
NSString *dateString = [self curentDateStringFromDate:[NSDate date] withFormat:@"dd-MM-yyyy"];
NSString *timeString = [self curentDateStringFromDate:[NSDate date] withFormat:@"hh:mm:ss"];
NSString *hoursString = [self curentDateStringFromDate:[NSDate date] withFormat:@"h"];
In the Foundation framework
, the class to use for this task (in either direction) is NSDateFormatter
Refer here
The code below convert GMT to IST.
NSString *inDateStr = @"2000/01/02 03:04:05";
NSString *s = @"yyyy/MM/dd HH:mm:ss";
// about input date(GMT)
NSDateFormatter *inDateFormatter = [[NSDateFormatter alloc] init];
inDateFormatter.dateFormat = s;
inDateFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
NSDate *inDate = [inDateFormatter dateFromString:inDateStr];
// about output date(IST)
NSDateFormatter *outDateFormatter = [[NSDateFormatter alloc] init];
outDateFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"IST"];
outDateFormatter.dateFormat = s;
NSString *outDateStr = [outDateFormatter stringFromDate:inDate];
// final output
NSLog(@"[in]%@ -> [out]%@", inDateStr, outDateStr);

Suresh D
- 4,303
- 2
- 29
- 51
-
thanx for help @Suresh.D brother. GMT to IST is done thank you for help. but still my main question is that how can I get Date, Hours from that Timestamp. If I'll get help that would be big help. – dmaulikr Apr 06 '17 at 06:33
-
@MaulikDesai updated my answer please go and check, Accept the answer if you got it. Thanks in Advance. – Suresh D Apr 06 '17 at 08:19