convert time to 12hr format - iphone

Please tell me how to convert time to 12 hr format..
I have a string which i have converted to date but it is in 24 hr format....

You could use the 24 hour clock format instead of the 12 hour format. So use a NSDateFormatter with H in the format string. Using one of the predefined formats will end up using your systems localization which probably is the 12 hour format.
NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
[inputFormatter setDateFormat:#"HH:mm"];
another solution is presented here using regex:
24hr to 12hr time convert, insert character to string

considering you have an hour int named time...
if (time>12)
time=time-12;

When you say you've converted it to a date, what do you mean? Do you mean you have it in an NSDate object? If so, the date is not in 24hr date format. NSDate does not concern itself with how the date will look on-screen.
The formatting comes from your localisation settings. Which is how it should be. I don't want times in 12hr format any more than you want them in 24hr.
But to directly answer the question, you may have to write your own formatting code if you want to override the defaults. I had to do this to work around bugs in NSDateFormatter when I received dates from a third party data source.

To change the date format from 12 to 24 hours or from 24 to 12 simply change hh:mm to HH:mm format where h is for 12 hours and H is for 24 hours.
Remember to change your location.
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"HH:mm"];

Related

Unknown NSDate format

I have the following string to convert to NSDate:
2011-04-27 20:50:09.000002.
I have the following code for the dateformatter:
[inputFormatter setDateFormat:#"YYYY-mm-dd HH:mm:ss"];
but I'm unsure of what to put after the seconds.
Keep in mind, I have multiple of these strings, with different numbers at the back.
Date format strings in OS 10.6 (and, I believe, in iOS4+) are based on the Unicode spec TR35-10 : have a look at their date formatting strings.
What you need in this case is fractional seconds: symbol S. Something like...
[inputFormatter setDateFormat:#"YYYY-mm-dd HH:mm:ss.SSSSSS"];

obj-c: how to parse a "human friendly" date?

I've read a many things about parsing date in obj-c, but I can't find anything dealing with dates like "Mon, 25 Apr 2011 11:53 am CDT"... I'd like to convert it to NSDate in a smart way but I'm running out of ideas.
Thanks.
-[NSDateFormatter dateFromString:]
You'd probably use #"EEE, d MMM yyyy hh:mm a zzz" as your date format string.
And by the way, googling "convert string to date objective-c" yields thousands of hits that have correct answers.
NSDateFormatter is a pretty complete class that covers all "human-friendly" date scenarios.
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html
There's also several questions on stackoverflow that seem very similar to your question, so a quick search should help you out.

how to change the date format in the objective-c

after selecting the date from the date picker, i am getting the date in the format in view like yy/mm/dd .
i want to the date format in my view like mm/dd/yy.
how can i do this.
You can use the following snippet to convert the date to the format you desire. Note that month use uppercase 'MM' in the format:
[dateFormatter setDateFormat:#"MM/dd/yy"];
dateString = [dateFormatter stringFromDate:theDate];
For a full list of the formats available, have a look at the Unicode Standard. With a few minor exceptions, these formats are all supported on the iPhone.

How to change format of date/time?

I have this date and time format:
2010-05-19 07:53:30
and would like to change it to:
Wednesday # 7:53PM 5/19/2010
I'm doing this, which gets the current format:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"yyyy-MM-dd HH:mm:ss";
but when I change the format, I end up with a null. For example:
formatter.dateFormat = #"hh:mm tt MM-dd-yyyy";
date = [formatter stringFromDate:formattedDate];
date will be null. I want to put the end result into an NSString. It would be nice if time and date could come out as separate properties so I can arrange them however I like. Any ideas on how I can change the formatting?
I think your formatting string is the problem. You should only use the characters you find in the table in UTS#35 Date Format Patterns. I tried your code and while the time hh:mm displays correctly, formatting stops at tt - not in the table!
If you really want characters in the format string that are not in the table you can escape them, like hh 'o''clock' a, zzzz - produces format like "12 o'clock PM, Pacific Daylight Time".
It would be nice if time and date could come out as separate properties so I can arrange them however I like. Any ideas on how I can change the formatting?
You have things backwards. If this is a date/time to be displayed to the user, you need to present it how the user wants it, not how you want it. For instance, most people outside the USA will be confused by MM-dd-yyyy particularly if the day is less than 13. Consider using -setDateStyle: and -setTimeStyle:. That way, the display string will come out as the user expects.

obj-c : iphone dateformatter in 12-hours clock format

i want in my application to use the 12 hour clock format so the user can write 15:00
for 3:00 pm so i don't need to create a button am and pm. Thanks
Probably you want to use the 24 hour clock format instead of the 12 hour format. So use a NSDateFormatter with H in the format string.
Using one of the predefined formats will end up using your systems localization which probably is the 12 hour format.
NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
[inputFormatter setDateFormat:#"HH:mm"];