IOS: NSDateFormatter for region format Italy - ios

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.

Related

Convert utc date into 12 hours local date?

My date and time is 20-Nov-2019 21:09 Which is in UTC 24 hours format. now I want to convert it into local time in 12 hours formate. 30-Nov-2019 08:00 AM like this.
My code is :
// create dateFormatter with UTC time format
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MMM-yyyy HH:mm"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
// change to a readable time format and change to local time zone
[dateFormatter setDateFormat:#"dd-MMM-yyyy HH:mm a"];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
NSString *timestamp = [dateFormatter stringFromDate:date];
My code when i send my local time 12 formate into 24 hours UTC
-(NSString *)getUTCFormateDate:(NSDate *)localDate
{
// NSLog(#"%#", localDate);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:#"UTC"];
[dateFormatter setTimeZone:timeZone];
[dateFormatter setDateFormat:#"dd-MMM-yyyy HH:mm"];
NSLocale *twelveHourLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
dateFormatter.locale = twelveHourLocale;
NSTimeInterval timeZoneoffset = [[NSTimeZone defaultTimeZone] secondsFromGMT];
NSTimeInterval utcTimeInterval = [localDate timeIntervalSinceReferenceDate] - timeZoneoffset;
NSDate *utcCurrentDate = [NSDate dateWithTimeIntervalSinceReferenceDate:utcTimeInterval];
NSString *dateString = [dateFormatter stringFromDate:utcCurrentDate];
// NSLog(#"dateString %#", dateString);
return dateString;
}
-(NSDate *)getUTCDate:(NSString *)currentDate{
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd-MMM-yyyy HH:mm"];
NSDate *date1 = [dateFormat dateFromString:currentDate];
if (date1 == nil){
[dateFormat setDateFormat:#"dd-MMM-yyyy hh:mm a"];
date1 = [dateFormat dateFromString:currentDate];
}
return date1;
}
I think you are doing too much. The format "hh" is the hour in 12-hour format, HH is 24-hour format. You should not have to set the locale for that (though setting to en_US_POSIX does avoid the user's 24-hour preference in the [NSLocale currentLocale] instance which can override that on iOS).
NSDate is an absolute instance in time. You need to apply a calendar and time zone with an NSDateFormatter to get numeric year/month/day etc. values out of it, but you don't need to adjust the offset to the reference date (that is changing the actual date, not just reformatting it in a different time zone).
NSString *utcString = #"20-Nov-2019 21:09";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.timeZone = [NSTimeZone timeZoneWithName:#"UTC"];
formatter.locale = [NSLocale localeWithLocaleIdentifier:#"en_US_POSIX"];
formatter.dateFormat = #"dd-MMM-yyyy HH:mm";
NSDate *date = [formatter dateFromString:utcString];
formatter.timeZone = [NSTimeZone localTimeZone]; // GMT-5 for me
formatter.dateFormat = #"dd-MMM-yyyy hh:mm a";
NSLog(#"date: %#", [formatter stringFromDate:date]);
// date: 20-Nov-2019 04:09 PM

How to convert UTC date string to local time

I currently get time as UTC and I want it to be convert into local time zone
NSDate * startDate;
Example strDate - 14/9/2017 7.28 Am
Required format - 14/9/2017 1.52 pm
I need help with objective c.
NSDate * startDate ; //contain utc date
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, MMM d, yyyy - h:mm a"];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
NSString *timestamp = [dateFormatter stringFromDate:startDate];
but this is not correct time but date is correct
First convert NSString to NSDate.
NSDateFormatter *DateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *timeZoneEDT =[NSTimeZone timeZoneWithName:#"GMT"];
[DateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[DateFormatter setTimeZone:timeZoneEDT];
[DateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss.SSS"];
NSDate *CurrentDate = [DateFormatter dateFromString:DateStr];
Then convert GMT time to local time.
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithName:#"Asia/Kolkata"];
[dateFormatter setTimeZone:sourceTimeZone];
NSString *dateRepresentation = [dateFormatter stringFromDate:dateFromString];
#athira try this. it works for me.
NSDate *startDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd/M/yyyy h.mm a"];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
NSString *timestamp = [dateFormatter stringFromDate:startDate];
NSLog(#"date = %#",timestamp); //date = 14/9/2017 4.34 PM
it will print current time with proper GMT+5:30 and i have used localTimeZone.

Date Formatter not working?

I want to change my date format from "MM/dd/YYYY HH:mm:ss" to "EEE dd MMM yyyy hh:mm a", but the code I am using not working for example if my input is 11/30/2016T01:04:30 I am getting the month changed as December, can any one help where is the mistake?
NSString * date = #"11/30/2016T01:04:30";
date = [date stringByReplacingOccurrencesOfString:#"T" withString:#" "];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"MM/dd/YYYY HH:mm:ss"];
NSDate *myDate = [df dateFromString: date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE dd MMM yyyy hh:mm a"];
NSString *dayName= [dateFormatter stringFromDate:myDate];
NSLog(#"the converted Day %#",dayName);
no need of this
// date = [date stringByReplacingOccurrencesOfString:#"T" withString:#" "];
use like
NSDateFormatter *df = [[NSDateFormatter alloc] init];
NSString * date = #"11/30/2016T01:04:30";
[df setDateFormat:#"MM/dd/yyyy'T'HH:mm:ss"];
NSDate *myDate = [df dateFromString: date];
[df setDateFormat:#"EEE dd MMM yyyy hh:mm a"];
NSString *dayName= [df stringFromDate:myDate];
NSLog(#"the converted Day %#",dayName);
Output:
the converted Day Wed 30 Nov 2016 01:04 AM

coversion of date and time from 31 May 2016 04:30 PM(current format) to 2016-05-31T16:30:00.000+05:30(Required format)

hi can any on help me in achiving this is the string which i have 31 May 2016 04:30 PM(NSSTring) 2016-05-31T16:30:00.000+05:30(Required Format)
NSString *dateString = dateandtime;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
used the following code but returns nil
The current format string does not contain any time zone information, so you have to set the time zone in the date formatter as well as the locale to be able to parse the string independently from the current locale.
The input format is dd MMM yyyy hh:mm a
NSString *dateString = #"31 May 2016 04:30 PM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.locale = [NSLocale localeWithLocaleIdentifier:#"en_US_POSIX"];
dateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:19800];
dateFormatter.dateFormat = #"dd MMM yyyy hh:mm a";
NSDate *dateFromString = [dateFormatter dateFromString:dateString];
dateFormatter.dateFormat = #"yyyy-MM-dd'T'HH:mm:ss.SSSZ";
NSString *stringFromDate = [dateFormatter stringFromDate:dateFromString];
Some how I tried and worked
NSString *myString = dateandtime;
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"dd MMM yyyy hh:mm a";
NSDate *yourDate = [dateFormatter dateFromString:myString];
dateFormatter.dateFormat = #"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZ";
NSLog(#"%#",[dateFormatter stringFromDate:yourDate]);
Whatever you do, test that your code works if the user doesn't use an Indian timezone, and if the user uses 24 hour time instead of AM/PM on their phone. I assume your "dateandtime" was created by converting an NSDate to an NSString; it would be much better to not do that but start with the original NSDate.
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];
NSString *currentDateString = #"2016-05-31T16:30:00.000+05:30";
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZ"];
NSDate *currentDate = [dateFormatter dateFromString:currentDateString];
NSLog(#"CurrentDate:%#", currentDate);
you can use this code.

iphone How to convert today date and time to GMTFormat(10 digits)

I have locale date and time I am using the following code
NSDate *today = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"d MMM yyyy hh:mm:ss a"];
NSString *currentTime = [dateFormatter stringFromDate:today];
NSLog(#"current time:%#",currentTime);
Here i got localTime
Now i am converting local time to GMT Time
I am using the following code
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateFormat:#"hh:mm:ss MM dd yyyy "];
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter1 setTimeZone:sourceTimeZone];
NSDate *dateFromString = [dateFormatter dateFromString:currentTime];
NSDate *dateFromString = [dateFormatter dateFromString:currentTime];
Now my problem is i want to convert this GMT date into 10digit format like 1362468453
For that purpose i am using
[dateFormatter setDateFormat:#"d MMM yyyy hh:mm:ss a"];
How to get GMT Time in tendigits format
Use timeIntervalSince1970 function for your date to get epoch time.
NSLog(#"Epoch Time : %f", [dateFromString timeIntervalSince1970]);
Check this NSDate Class Reference

Resources