I have Issue in dateformatter. I add my code as below:
NSDate *currentDate = [NSDate date];
NSLog(#"Current Date: %#",currentDate); //Current Date: 2016-10-14 08:07:15 +0000
NSTimeZone *timeZone = [NSTimeZone localTimeZone];
NSString *tzName = [timeZone name];
NSLog(#"Current TimeZone: %#",tzName); //Current TimeZone: Asia/Kolkata
NSTimeZone *systemTimeZone = [NSTimeZone systemTimeZone];
NSString *SyTZName = [systemTimeZone name];
NSLog(#"System TimeZone: %#",SyTZName); //System TimeZone: Asia/Kolkata
NSString *strCurrentDate = [NSString stringWithFormat:#"%#",[NSDate date]];
NSLog(#"Current Date: %#",strCurrentDate);//Current Date: 2016-10-14 08:07:47 +0000
NSDateFormatter* localDateFrm = [[NSDateFormatter alloc] init] ;
[localDateFrm setTimeZone:[NSTimeZone timeZoneWithName:#"IST"]];
[localDateFrm setDateFormat:#"dd-MM-yyyy hh:mm a"];
NSString* local_string = [localDateFrm stringFromDate:currentDate];
NSLog(#"local_string: %#",local_string);//local_string: 14-10-2016 01:37 PM
NSDateFormatter* local_dateFormatter = [[NSDateFormatter alloc] init] ;
[local_dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"IST"]];
[local_dateFormatter setDateFormat:#"dd-MM-yyyy hh:mm a"];
NSDate *date = [local_dateFormatter dateFromString:local_string];
NSLog(#"date : %#",date);//date : 2016-10-14 08:07:00 +0000
at last I need 01:37 PM but I get 08:07:00 +0000.when I convert string into NSDate its not give me expected reply(01:37 PM). so the time is different.
so please help me for this.
Thanks.
NSDate will always return utc or gmt date, if you want it to in your local timezone then you need to convert it to string with stringFromDate method of NSDateFormatter.
So, when you want to display your date with your local timezone just convert it to string and then show. And when you want it to as NSDate you can easily convert string to date with dateFromString method and it will return date in utc or gmt again.
And usually you need to display your date in label or any UI so you must need string, you can't show nsdate on label. So, this is standard approach to deal with NSDate
Update :
You can use this date as localnotification's firedate also. execute below demo,
NSString *str = #"2016-10-14 08:07:00";
NSDateFormatter *df = [[NSDateFormatter alloc]init];
[df setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
NSDate *date = [df dateFromString:str];
NSLog(#"date : %#",[df dateFromString:str]);
UILocalNotification *notification = [[UILocalNotification alloc]init];
notification.fireDate = date;
notification.alertTitle = #"title";
NSLog(#"notification : %#",notification);
and check log for notification, it will show firetime as per your local timezone!!
Second thing you can set timezone also for notification like notification.timeZone = ...;
Related
I had search on google how to get GMT Time in Objective-C, and I got this :
+ (NSString *)GetGMTTime{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"yyyy-MM-dd'T'HH:mm";
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter setTimeZone:gmt];
return [dateFormatter stringFromDate:[NSDate date]];}
Is there any way to add or set GMT Time ? (GMT+5)
The most flexible way is to use timeZoneForSecondsFromGMT. This is more typo proof than using strings like GMT+X.
Here is a fuly working example:
+ (NSString *)GetGMTTime{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"yyyy-MM-dd'T'HH:mm";
NSTimeZone *gmt = [NSTimeZone timeZoneForSecondsFromGMT:(60*60*5)];
[dateFormatter setTimeZone:gmt];
return [dateFormatter stringFromDate:[NSDate date]];
}
It returns 2016-08-29T12:13 (when GMT time is 2016-08-29T7:13)
Note: 60*60*5 means 5 hours X 60 minutes X 60 seconds
Can try like this.
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:#"GMT+5:30"];
If you create NSDate instance then it will return date in UTC or GMT format by default.
Now when you convert this date to string by any date formatter then returned string (date) will be in local timezone (i.e. device's time zone).
You can create custom timezone with it's name to get date in that timezone.
NSDate *date = [NSDate date]; //give current date in UTC or GMT
NSLog(#"current date in gmt : %#",date);
NSDateFormatter *df = [[NSDateFormatter alloc]init];
[df setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSString *dateInStrFormat = [df stringFromDate:date]; // this date(string format) will be in current timezone that was set in your device and
NSLog(#"current date in local or device's default timezone : %#",dateInStrFormat);
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:#"Asia/Kolkata"];
NSLog(#"timezone : %#",timeZone);
[df setTimeZone:timeZone];
NSString *dateInCustomTimeZone = [df stringFromDate:date]; // this will return date in Asia/Kolkata's timezone
NSLog(#"date in custom timezone : %#",dateInCustomTimeZone);
NSLog(#"timezone : %#",timeZone); will print something like, Asia/Kolkata (IST) offset 19800. 19800 is offset, if you divide it with 3600 then you will get difference with gmt like (+/- 5.30 etc)
Link for different timezone names
Or you can got timezone like,
NSTimeZone *timezone1 = [NSTimeZone timeZoneWithName:#"GMT+5:30"];
NSLog(#"timezone : %#",timezone1);
NSTimeZone *timeAone2 = [NSTimeZone timeZoneForSecondsFromGMT:60*60*5.5];
NSLog(#"timezone : %#",timeAone2);
I know this is a very question and asked multiple times before.But this is something i get stuck every time.
Now, i want to convert a date string from web service which is in Argentina local zone(UTC-3:0) and want to convert this that to device local time zone(Suppose UTC+5:30). Here is my code
-(NSDate *)getLocaDateStringFromDate:(NSString *)sourceDate andSourceTime:(NSString *)sourceTime
{
//sourceDate is #"2015-10-16"; and sourceTime is #"00:00:00"
static NSDateFormatter* df = nil;
if (!df) {
df = [[NSDateFormatter alloc]init];
df.dateFormat = #"yyyy-MM-dd HH:mm:ss";
}
NSString* source = [sourceDate stringByAppendingString:[NSString stringWithFormat:#" %#", sourceTime]];
NSTimeZone *sourceZone = [NSTimeZone timeZoneWithAbbreviation:#"ART"];
[df setTimeZone: sourceZone];
NSDate *ds = [df dateFromString:source];
NSLog(#"sourceZone time is %#" , [df stringFromDate: ds]);//sourceZone time is 2015-10-16 00:00:00, correct!
NSTimeZone *localTimeZone = [NSTimeZone systemTimeZone];
[df setTimeZone: localTimeZone];
NSLog(#"local time is %#" , [df stringFromDate: ds]);//local time is 2015-10-16 08:30:00,, correct
NSString* str = [df stringFromDate: ds];
return [df dateFromString:str]; //this return 2015-10-16 03:00:00 +0000, why?
}
My question is that why my method return date in UTC, regardless of my time zone set to systemTimeZone. I get correct string but date is incorrect.
Any help would be appreciated.
1.) First, create an NSDateFormatter to get the NSDate sent from the server.
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
2.) Convert the date string to a NSDate.
NSDate* sourceDate = [dateFormatter dateFromString:dateString];
3) Specify ,Local and Destination timezone in which you want to convert your date.
For getting timezone of your source date:
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
For getting User timezone :
NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];
4.) Calculate the inverval between source timezone and user timezone as given bellow:
NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate];
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
NSDate* destinationDate = [[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate];
5.) Now, create an NSDateFormatter for formatting date in which you want to show.
NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormat setDateFormat:#"M/d/yy 'at' h:mma"];
NSString* localTime = [dateFormat stringFromDate:destinationDate];
NSLog(#"localTime:%#", localTime);
Hope it is useful for you :)
I used this snippet to convert a given date to a string. But if the date happens to have a time of less than 5:00 then the result is a prior date. For example, this date: "07-01-15 05:00:00 +0000" will work correctly. But a "07-01-15 04:49:00 +0000" will have a prior date as the result.
Any suggestions?
+(NSString *)stringFromGivenDate:(NSDate *)date
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"MM-dd-yy";
NSString *dateString = [dateFormatter stringFromDate:date];
NSLog(#"date: %#", dateString);
return dateString;
}
NSDateFormatter by default sets the time zone to your locale time zone.
But while converting date to string changes the time to GMT time.
So you have to set the time zone using
dateFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:#"GMT"]
or whatever timezone you want(like #"GMT+5:30").
So your code will be like:
+(NSString *)stringFromGivenDate:(NSDate *)date
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"MM-dd-yy";
dateFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:#"GMT"]
NSString *dateString = [dateFormatter stringFromDate:date];
NSLog(#"date: %#", dateString);
return dateString;
}
I have string - Dec 13, 10:00 AM IST
How to convert this string to date anyone suggest ?
I created this date formatter but getting null -
NSDateFormatter *userTimeFormatter = [[NSDateFormatter alloc] init];
[userTimeFormatter setDateStyle:NSDateFormatterMediumStyle];
[userTimeFormatter setTimeStyle:NSDateFormatterShortStyle];
userTimeFormatter.doesRelativeDateFormatting = YES;
[userTimeFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSDateFormatter *userTimeFormatter = [[NSDateFormatter alloc] init];
NSString *dateString = #"Dec 13, 10:00 AM IST";
NSTimeZone *timeZone = [NSTimeZone timeZoneWithAbbreviation:#"IST"];
[userTimeFormatter setTimeZone:timeZone];
dateString = [dateString stringByReplacingOccurrencesOfString:#" IST" withString:#""];
[userTimeFormatter setDateFormat:#"MMM d, hh:mm a"];
NSDate *date = [userTimeFormatter dateFromString:dateString];
NSLog(#"india date : %#", date);
This will give you the IST TimeZone NSDate, from this date you can convert to system timezone with:
[userTimeFormatter setTimeZone:[NSTimeZone systemTimeZone]];
NSDate *myDate = [userTimeFormatter dateFromString:dateString];
NSLog(#"my date : %#", myDate);
Output:
india date : 2000-12-13 04:30:00 +0000
my date : 2000-12-13 06:00:00 +0000
I have weird result trying to output NSDate object from NSString.
My NSString is: 1976-06-11
My method to convert is:
-(NSDate*)dateFromString:(NSString *)dateString{
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd"];
NSDate *date = [dateFormat dateFromString:dateString];
return date;
}
But it output 1976-06-10 21:00:00 +0000
How could that happen? Difference in 1 day.
You have date in UTC format. Use this code to converting your date to local time:
NSTimeInterval seconds; // assume this exists
NSDate *ts_utc = [NSDate dateWithTimeIntervalSince1970:seconds];
NSDateFormatter *utcFormatter = [[NSDateFormatter alloc] init];
utcFormatter.timeZone = [NSTimeZone timeZoneWithName:#"UTC"];
utcFormatter.dateFormat = #"yyyy.MM.dd G 'at' HH:mm:ss zzz";
NSDateFormatter *localFormatter = [[NSDateFormatter alloc] init];
localFormatter.timeZone = [NSTimeZone timeZoneWithName:#"EST"];
localFormatter.dateFormat = #"yyyy.MM.dd G 'at' HH:mm:ss zzz";
NSString *utcDateString = [utcFormatter stringFromDate:ts_utc];
NSString *LocalDateString = [localFormatter stringFromDate:ts_utc];
Or you can use [NSTimeZone defaultTimeZone] to prevent hardcoded strings for timezone names. This method returns the system time zone, If no default time zone has been set.
You can use following methods to convert an UTC date string into UTC date and local date
- (NSDate *)convertIntoGMTZoneDate:(NSString *)dateString
{
NSDateFormatter *gmtFormatter = [[NSDateFormatter alloc]init];
[gmtFormatter setDateStyle:NSDateFormatterFullStyle];
[gmtFormatter setTimeStyle:NSDateFormatterFullStyle];
[gmtFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
return [gmtFormatter dateFromString:dateString];
}
- (NSDate *)convertIntoSystemZoneDate:(NSString *)dateString
{
NSDateFormatter *systemZoneFormatter = [[NSDateFormatter alloc]init];
[systemZoneFormatter setDateStyle:NSDateFormatterFullStyle];
[systemZoneFormatter setTimeStyle:NSDateFormatterFullStyle];
[systemZoneFormatter setTimeZone:[NSTimeZone systemTimeZone]];
return [systemZoneFormatter dateFromString:dateString];
}
func dateFromString(dateString: String) -> NSDate {
// Convert string to date object
var dateFormat = NSDateFormatter()
dateFormat.dateFormat = "yyyy-MM-dd"
dateFormat.timeZone = NSTimeZone(name: "UTC")
var date = dateFormat.dateFromString(dateString)!
print(date)
return date
}
output :
1976-06-11 00:00:00 +0000
If you debug code, it shows 1 day difference but after run you will find the actual date which is you enter.
It works for me.I think it will helps you.
Thank you