Like if I am using the code.
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
[df setTimeZone:[NSTimeZone systemTimeZone]];
[df setDateFormat:#"dd.MM.yyyy"];
NSDate *today = [NSDate date];
NSString *log_date = [df stringFromDate:today];
//Generate Log Current Time.
[df setDateFormat:#"hh:mma"];
NSString *log_currenttime = [[df stringFromDate:today] lowercaseString];
NSLog(#"Log Date %#",log_date);
NSLog(#"Log Current Time %#",log_currenttime);
My log shows me :
Log Date 19.12.2011
Log Current Time 03:18pm
At least I can remove the Time Padding.
Change your format from hh:mma to h:mma
//Generate Log Current Time.
[df setDateFormat:#"h:mma"]; // Changed from hh:mma to h:mma
NSString *log_currenttime=[[df stringFromDate:today] lowercaseString];
NSLog(#"Log Current Time %#",log_currenttime);
This will output
Log Current Time 3:18pm
As Eric wrote, use h:mm will fail for example on czech calendar.
Only way to do that is use system formatter.
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
[df setTimeZone:[NSTimeZone systemTimeZone]];
[df setDateFormat:#"dd.MM.yyyy"];
[df setTimeStyle:NSDateFormatterNoStyle];
NSDate *today = [NSDate date];
NSString *log_date = [df stringFromDate:today];
//Generate Log Current Time.
[df setTimeStyle:NSDateFormatterShortStyle];
[df setDateStyle:NSDateFormatterNoStyle];
NSString *log_currenttime = [[df stringFromDate:today] lowercaseString];
NSLog(#"Log Date %#",log_date);
NSLog(#"Log Current Time %#",log_currenttime);
The problem with Aadhira's answer is that if the user then switches to metric the #h:mma formatting will override it and make 23:00 look like 11:00pm.
The padding comes from the users settings in Settings-->General-->International-->Region Format. For example if it is set to UK, you will get padding. If it is set to US you won't. You may want to respect this decision in your user interface.
Related
I need to convert an NSString to an NSDate, but the following code only works as long as the user's device isn't set to 24-Hour time.
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:#"MM/dd/yyyy hh:mm:ss a"];
NSString *aTime = #"2/11/2013 12:00:00 AM";
NSDate *aDate = [formatter dateFromString:aTime];
However, aDate returns null when the device is in 24-Hour time. Any help?
I think you're probably seeing a side effect of the behaviour described by QA1480. Apple has resolved what it presumably thought was developers not obeying locales properly by modifying your prescribed date format unless you explicitly say not to.
You can achieve that by adding:
formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
/* and autorelease if not using ARC */
Which basically says 'the date format I just set is explicitly what I want, please don't modify it in any way'.
I think the following snippet might helps you out.
NSString *dateString = #"09:34 AM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"dd-MM-yyyy";
NSString *stringFromDate = [dateFormatter stringFromDate:[NSDate date]];
dateFormatter.dateFormat = #"dd-MM-yyyy hh:mm a";
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
NSDate *returnDate = [dateFormatter dateFromString:[NSString stringWithFormat:#"%# %#", stringFromDate, dateString]];
try this code
NSString *string = #"2015-11-30 15:00:00";
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"yyyy-MM-dd HH:mm:ss";
[formatter setTimeZone:[NSTimeZone localTimeZone]];
NSDate *date = [formatter dateFromString:string];
date: 2015-11-29 21:30:00 +0000
its in UTC format you can modify this as you need!
Hi Experts of the world,
I ran into a very weird problem:
I am formatting a string representing time from 00-23 (as returned by a Google service) in the following manner:
(passing in a string of lets say 14, should output either 14:00 or 2:00 PM, depends on user local)
+(NSString *) formatTime: (NSString *)timeToBeFormatted {
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"HH"];
NSDate *date = [[NSDate alloc] init];
date = [dateFormat dateFromString:timeToBeFormatted];
// Convert date object to desired output format
[dateFormat setTimeStyle:NSDateFormatterShortStyle];
timeToBeFormatted = [dateFormat stringFromDate:date];
return timeToBeFormatted;
}
Everything works fine in all locals worldwide.
However, ONLY if a user has his TIME format set on 12h in a local where the default is 24h the formatter will return NULL ONLY for vales between 12-23.. Pretty weird i would say!
Example:
before formatter 12
after 12:00 AM
before formatter 13
after (null)
Any ideas why this could happen?
Thanks!
Solved! (inspired by the answers above)..
To solve the issue i am creating a specific Locale, then phrasing the stringToDate using this locale. Then i am creating another Locale with the default users preferences and phrasing the dateBackToString using that locale..
+(NSString *) formatTime: (NSString *)timeToBeFormatted
{
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
//ADDED//
NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
[dateFormat setLocale:enUSPOSIXLocale];
[dateFormat setDateFormat:#"HH"];
NSDate *date = [[NSDate alloc] init];
date = [dateFormat dateFromString:timeToBeFormatted];
//ADDED//
NSLocale *defualtLocale = [[NSLocale alloc] init];
[dateFormat setLocale:defualtLocale];
[dateFormat setTimeStyle:NSDateFormatterShortStyle];
timeToBeFormatted = [dateFormat stringFromDate:date];
return timeToBeFormatted;
}
I guess its quite costly for older devices but in the era of ARC and strong phones it works ;)
NSDateFormatter uses the current locale and time settings for parsing (and outputting) time. If you want to use a specific time format, set the locale for the date formatter yourself.
dateFormat.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
Also, creating date formatter is expensive, if you call this function often you should cache the date formatter in a static variable.
I was also facing this issue before some time.
Use following code to formate your date as per your need.
+(NSDate *)getGMTDateToView:(NSDate *) availableDate formatter:(NSDateFormatter *)timeFormat {
NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
[timeFormat setLocale:enUSPOSIXLocale];
NSTimeInterval timeZoneOffset = [[NSTimeZone defaultTimeZone] secondsFromGMT];
NSTimeInterval gmtTimeInterval = [availableDate timeIntervalSinceReferenceDate] + timeZoneOffset;
[timeFormat setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
[timeFormat setDateStyle:NSDateFormatterShortStyle];
[timeFormat setTimeStyle:NSDateFormatterShortStyle];
enUSPOSIXLocale = nil;
return [NSDate dateWithTimeIntervalSinceReferenceDate:gmtTimeInterval];
}
I found above code from one of apple's document (I have modified(little bit) it as per my need) but unable to find this link right now.
I am developing one application. In that i write the below code for setting the DateFormat for current date. But it is working upto 9th month only. From 10th month onwards it gives the nil value. So please tell me how to solve this one. My code is as below:
NSDate *datestr = clInfo.cldrinfodate;
NSDateFormatter *dateFormat1 = [[NSDateFormatter alloc] init];
[dateFormat1 setDateFormat:#"MM/dd/yyyy hh:mma"];
NSString *date1 = [dateFormat1 stringFromDate:datestr];
you can try this one.
NSDate * mCurrentDate = [NSDate date];
NSLog(#"current date=%#",mCurrentDate);
NSDateFormatter *dt=[[NSDateFormatter alloc] init];
[dt setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSString *st=[dt stringFromDate:mCurrentDate];
I have XML with this value:
<LastModifiedOnDate>2011-04-02T00:00:00</LastModifiedOnDate>
I am trying to parse this on iPhone from NSString to NSDate but have no luck.
NSDateFormatter *formate = [[[NSDateFormatter alloc] init] autorelease];
[formate setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss"];
//NSString *strConcat = #"2010-09-24 17:30:00";
NSDate *date = [formate dateFromString:string];
You don't need all those apostrophes in your format string. The only ones you need are around the T.
NSDateFormatter *formate = [[[NSDateFormatter alloc] init] autorelease];
[formate setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];
// I recommend setting a timezone because it will use the system timezone and that
// can cause confusion with your data.
// [formate setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSString *strConcat = #"2010-09-24T17:30:00";
NSDate *date = [formate dateFromString:strConcat];
NSLog(#"Date: %#", date); // remember NSDate default to current timezone when logging to console
If you string have fraction of seconds, then add SSS to your string format: [formate setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS"];
For additional information on date formatting check out Unicode Technical Standard #35.
Replace your's SetDataFormat function with below and let me know for the result ...
[formate setDateFormat:#"yyyy-MM-ddTHH:mm:ss"];
NSString *string=#"2011-04-05T12:43:56.537";
NSDateFormatter *formate = [[[NSDateFormatter alloc] init] autorelease];
[formate setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss.SSS"];
NSDate *date = [formate dateFromString:string];
its working..
I need to convert a date in this string format:
"2011-01-12T14:17:55.043Z"
to a number like 1294841716 (which is the number of seconds [not milliseconds] since Jan. 1st, 1970). Is there an easy way to do this parsing?
Update: Here is the code I've got so far:
NSString *dateString = #"2011-01-12T14:17:55.043Z";
NSDateFormatter *inFormat = [[NSDateFormatter alloc] init];
[inFormat setDateFormat:#"yyyy-MM-ddTHH:mm:ss.nnnZ"];
NSDate *parsed = [inFormat dateFromString:dateString];
long t = [parsed timeIntervalSinceReferenceDate];
But t comes back as 0 every time.
Use NSDateFormatter to get a NSDate then use - (NSTimeInterval)timeIntervalSince1970 to get the seconds since 1970.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
NSDate *date = [formatter dateFromString:#"2011-01-12T14:17:55.043Z"];
NSLog(#"Date: %#", date);
NSLog(#"1970: %f", [date timeIntervalSince1970]);
NSLog(#"sDate: %#", [formatter stringFromDate:date]);
[formatter release];