date format is not working properly - ios

I am converting date as per timezone and i have one more requirement is date format. Now i am getting date in 12/15/14, 5:00 AM format but i want it in “18th Nov. 2014 at 5:15 PM” format.Event I am not getting proper output as per my date format.
Is this possible in "Objective C".My code is :
-(NSString *)dateFormater : (NSString *)dateStr
{
//NSString *dateStr = #"2014-11-30T21:25:00+00:00";
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssz"];
// [dateFormatter1 setDateFormat:#"dd MMM,yyyy'T'HH:mm:ssz"];
NSDate *date = [dateFormatter1 dateFromString:dateStr];
NSLog(#"date : %#",date);
NSTimeZone *currentTimeZone = [NSTimeZone localTimeZone];
NSTimeZone *utcTimeZone = [NSTimeZone timeZoneWithAbbreviation:#"UTC"];
NSInteger currentGMTOffset = [currentTimeZone secondsFromGMTForDate:date];
NSInteger gmtOffset = [utcTimeZone secondsFromGMTForDate:date];
NSTimeInterval gmtInterval = currentGMTOffset - gmtOffset;
NSDate *destinationDate = [[NSDate alloc] initWithTimeInterval:gmtInterval sinceDate:date];
NSDateFormatter *dateFormatters = [[NSDateFormatter alloc] init];
// [dateFormatters setDateFormat:#"dd-MMM-yyyy hh:mm"];
[dateFormatters setDateFormat:#"dd MMM, yyyy hh:mm a"];
[dateFormatters setDateStyle:NSDateFormatterShortStyle];
[dateFormatters setTimeStyle:NSDateFormatterShortStyle];
[dateFormatters setDoesRelativeDateFormatting:YES];
[dateFormatters setTimeZone:[NSTimeZone systemTimeZone]];
dateStr = [dateFormatters stringFromDate: destinationDate];
NSLog(#"DateString : %#", dateStr);
return dateStr;
}

You format string dd MMM, yyyy hh:mm a is correct but after setting the format string you are changing it again.
Remove these lines:
[dateFormatters setDateStyle:NSDateFormatterShortStyle];
[dateFormatters setTimeStyle:NSDateFormatterShortStyle];
But in general I would not use a static format string, at least not if you are releasing the app world wide. Since most countries have different localization for presentation purposes you are better of using :
dateStr = [NSDateFormatter localizedStringFromDate:date dateStyle:NSDateFormatterMediumStyle timeStyle:NSDateFormatterShortStyle];

Related

How to get the current time based on TimeZone in objective c?

How to get the current time based on TimeZone in objective c?
example-> Current time for Asia/Kolkata.
Try this..
NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormatter setDateFormat:#"dd/MM/yyyy HH:mm"];
//Create the date assuming the given string is in GMT
dateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
//Create a date string in the local timezone
dateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:[NSTimeZone localTimeZone].secondsFromGMT];
NSString *localDateString = [dateFormatter stringFromDate:[NSDate date]];
NSLog(#"date = %#", localDateString);
and if you want specific timezone date then see below code..
NSDate *myDate = [NSDate date];
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateStyle:NSDateFormatterLongStyle];
[dateFormatter1 setTimeStyle:NSDateFormatterLongStyle];
[dateFormatter1 setDateFormat:#"dd/MM/yyy hh:mm a"];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName: #"Asia/Calcutta"];
[dateFormatter1 setTimeZone:timeZone];
NSString *dateString = [dateFormatter1 stringFromDate: myDate];
NSLog(#"%#", dateString);

How to convert my server time to other country time?

I am getting time from server is 2016/07/19--12:20:00
Now I have to change this time according to country.
For example If I am in Auckland then time has to like 2016/07/19--18:50:00
I found so many solutions in stack overflow but I can't solve this.
Please help me
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"YYYY/MM/dd--HH:mm:ss"];
NSDate* sourceDate = [dateFormatter dateFromString:model.tripEndTime];
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
NSTimeZone* destinationTimeZone = [NSTimeZone localTimeZone];
NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate];
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
NSDate* destinationDate = [[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate];
NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormat setDateFormat:#"YYYY/MM/dd--HH:mm:ss"];
NSString* localTime = [dateFormat stringFromDate:destinationDate];
NSLog(#"localTime:%#", localTime);
I tried with this code
Just convert the server time to UTC time, then convert to local country time. Just write a separate method for this
+(NSString*)convertLocaleTimeToChatTimeStampWithDateStr:(NSString *)strDate
{
NSDateFormatter *df = [DateHelper getDefaultTimeFormatter];
[df setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
[df setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
NSDate *date = [df dateFromString:strDate];
[df setDateFormat:#"dd MMM, hh:mm a"];
[df setTimeZone:[NSTimeZone localTimeZone]];
return [df stringFromDate:date];
}
+(NSDateFormatter*)getDefaultTimeFormatter
{
NSDateFormatter *df = [NSDateFormatter new];
[df setDateStyle:NSDateFormatterNoStyle];
[df setTimeStyle:NSDateFormatterShortStyle];
return df;
}
hope this will solve your problem. Thanks
It is better to use server timestamp. After getting timestamp convert it according to timezone

iOS NSDate get timeZone specific date?

I'm getting time in HH:mm:ss format from web service and it is in Argentina(GMT-3) time zone. I want to convert this time into full date (dd-MM-yyyy HH:mm:ss) and finally convert it into device local time zone's date.
Here is my code
-(NSString *)getLocalTimeStringFrom:(NSString *)sourceTime
{
static NSDateFormatter* df = nil;
static NSDateFormatter* df1 = nil;
if (!df) {
df = [[NSDateFormatter alloc]init];
df.dateFormat = #"dd-MM-yyyy HH:mm:ss";
}
if (!df1) {
df1 = [[NSDateFormatter alloc]init];
df1.dateFormat = #"dd-MM-yyyy";
}
NSTimeZone *sourceZone = [NSTimeZone timeZoneWithAbbreviation:#"ART"];
[df setTimeZone: sourceZone];
[df1 setTimeZone: sourceZone];
NSString *artDate = [df1 stringFromDate:[NSDate date]]; // get 29-09-2015
NSString* timeStamp = [artDate stringByAppendingString:[NSString stringWithFormat:#" %#",sourceTime]]; // get 29-09-2015 00:05:00, if sourceTime is 00:05:00
NSDate *art = [df dateFromString:timeStamp];
NSLog(#"ART date %#" , art);//ART date 2015-09-29 03:05:00 +0000
NSTimeZone *localTimeZone = [NSTimeZone systemTimeZone];
[df setTimeZone: localTimeZone];
NSLog(#"Local date %#" , [df stringFromDate: art]);
return [df stringFromDate: ds];
}
The problem is that i'm getting UTC date in art (as commented in code),please note that the value of art is 2015-09-29 03:05:00 +0000 which is in UTC and format is yyyy-MM-dd HH:mm:ss, but is should be in ART and dd-MM-yyyy HH:mm:ss. I tried some code from net but didn't get solution. What is wrong in this code?
I use this:
NSDateFormatter* serverDf = [[NSDateFormatter alloc] init];
[serverDf setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:-3*60*60]];
[serverDf setDateFormat:#"dd-MM-yyyy HH:mm:ss"];
NSDate* gmtDate = [serverDf dateFromString:timeStamp];
// DDLogVerbose(#"DateTime in GMT: %#", gmtDate);
NSDateFormatter* localDF = [[NSDateFormatter alloc] init];
[localDF setDateFormat:#"dd-MM-yyyy HH:mm:ss"];
[localDF setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"]];
NSString *localDate = [localDF stringFromDate:gmtDate];
May you just want a formate NSString , but not a NSDate,
Because I think NSDate will add a '+0000'
Try this.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];
NSDate *date = [dateFormatter dateFromString:yourString];
NSString *str = [dateFormatter stringFromDate:date];

24 hour time from date string

I am trying to get just the time and that too in 24 hour format from following string :
7/2/2015 2:30:00 PM
This is what I have tried :
-(NSString*)returnDate:(NSString*)dateString
{
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateFormat:#"MM/dd/yyyy hh:mm:ss aaa"];
NSDate *date = [dateFormatter1 dateFromString:dateString];
dateString = [date descriptionWithLocale:[NSLocale systemLocale]];
NSTimeZone *currentTimeZone = [NSTimeZone localTimeZone];
NSTimeZone *utcTimeZone = [NSTimeZone timeZoneWithAbbreviation:#"UTC"];
NSInteger currentGMTOffset = [currentTimeZone secondsFromGMTForDate:date];
NSInteger gmtOffset = [utcTimeZone secondsFromGMTForDate:date];
NSTimeInterval gmtInterval = currentGMTOffset - gmtOffset;
NSDate *destinationDate = [[NSDate alloc] initWithTimeInterval:gmtInterval sinceDate:date];
NSDateFormatter *dateFormatters = [[NSDateFormatter alloc] init];
[dateFormatters setDateFormat:#"HH:mm a"];
[dateFormatters setDateStyle:NSDateFormatterShortStyle];
[dateFormatters setTimeStyle:NSDateFormatterShortStyle];
[dateFormatters setDoesRelativeDateFormatting:YES];
[dateFormatters setTimeZone:[NSTimeZone systemTimeZone]];
dateString = [dateFormatters stringFromDate: destinationDate];
return dateString;
}
Output : 4:30
This is returning correct time but in 12 hour format. I want the time in 24 hour format.
Desired output : 16:30
There is way to much code in the question, try this:
NSString *dateString = #"7/2/2015 4:30:00 PM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormatter setDateFormat:#"MM/dd/yyyy hh:mm:ss aaa"];
NSDate *date = [dateFormatter dateFromString:dateString];
[dateFormatter setDateFormat:#"HH:mm"];
dateString = [dateFormatter stringFromDate: date];
NSLog(#"dateString: %#", dateString);
Output:
dateString: 16:30

IOS: NSDateFormatter for region format Italy

I have tested my app with regiorn format USA and the date display is correct. When region changed to Italy where I live now contains null value.
My starter string date is:
- "May 2, 2013 6:46:33 PM"
The result date correct is:
- "02/05/2013 18:46:33"
Here my code:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter setDateFormat:#"MM dd, yyyy hh:mm:ss a"];
NSDate *dateFromString;
dateFromString = [dateFormatter dateFromString:dataStr];
[dateFormatter setDateFormat:#"dd/MM/yyyy HH:mm:ss"];
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
NSString *stringFromDate = [dateFormatter stringFromDate:dateFromString];
If your starter string is May 2, 2013 6:46:33 PM then you have two issues:
Your format string MM dd, yyyy hh:mm:ss a does not match your string. It needs to be MMMM dd, yyyy hh:mm:ss a. The use of MM is for numeric months. Use MMM for abbreviated month names and use MMMM for full month names.
Your date string has month names in English. If your device is setup for Italy then it won't properly parse the month names since it will expect the months names to be in Italian.
Your code should be:
NSString *dateStr = #"May 2, 2013 6:46:33 PM";
NSDateFormatter *inputDateFormatter = [[NSDateFormatter alloc] init];
[inputDateFormatter setDateFormat:#"MMMM dd, yyyy hh:mm:ss a"];
[inputDateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"]];
NSDate *dateFromString = [inputDateFormatter dateFromString:dataStr];
NSDateFormatter *outputDateFormatter = [[NSDateFormatter alloc] init];
[outputDateFormatter setDateFormat:#"dd/MM/yyyy HH:mm:ss"];
NSString *stringFromDate = [outputDateFormatter stringFromDate:dateFromString];
Use this:
NSDate* sourceDate = your_date
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];
NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate];
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
NSDate* destinationDate = [[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate];
Hope it helps you.

Resources