Date Formatting in iPhone SDk - cocoa-touch

I'm working on one iPhone application that includes date formatting. Here i'm getting date string from the server that i need to change and display of the formatted date.
Please find the below date m getting from the server
Server Date: Sat Apr 23 16:35:33 +0000 2011
This date i need to display like 2:35 AM Apr 23rd
Can anyone please help in this.
Thanks in advance.

You should use an NSDateFormatter to parse the date string you get from the server and get the corresponding NSDate (via -[NSDateFormatter dateFromString:]). You would then use a second NSDateFormatter to convert the NSDate to an NSString with the desired date format. Unless you absolutely require a very specific display format, you should use +[NSDateFormatter dateFormatFromTemplate:options:locale:] to get a date format that a) includes all the details you want it to include and b) respects the user’s locale settings. For instance, some users might prefer a 24-hour format instead of the 12-hour format with an AM/PM designation.

Related

NSDate dateWithTimeIntervalSince1970 displays wrong timestamp to console

NSDate *createDate = [NSDate dateWithTimeIntervalSince1970:1376460694.103];
NSLog(#"createDate %#",createDate);
I am using the above code to get the current date and time,when I put break point at createDate,It shows correct time stamp value,but NSLog(#"createDate %#",createDate) statement is printing the date as 2013-08-14 06:11:34 +0000.
How to get the correct result?
The date is correct. When printing to the console the description of the date is used and that uses your system locale so it applies your time zone to the date before printing.
When you want to display the time you need to use a date formatter to convert the date into a string. The important part is setting the locale / time zone that the formatter uses.
Take a read of this and this.

Objective-C format date and string

guys
I have two question to ask, they're easy, but bothering me for a while.
I access my .NET test WebService, and it return two parameters to me.
One is a date data just like "/Date(1332399761677+0800)/", and I dont know
how to format it to the normal date format.
Two is a NSString data looks like "12000.00000",and I want to change it to
the format like this:"12000.00".
So,please help me with this two problems. Thank you in advance.
1332399761677 looks like a Unix date, so if you grab that part of the string, use NSString doubleValue to turn it into a double, then use NSDate dateWithTimeIntervalSince1970:, you should be able to get a date. +0800 looks like a timezone, but you wouldn't need the timezone to get the date given a Unix date: 1332399761677 would specify a specific point in time, irrespective of timezones.
As for "12000.00000", you would use doubleValue to make it into a double, make an NSNumberFormatter with its maximumFractionDigits and minimumFractionDigits set to 2, then use stringFromNumber:.

CFAbsoluteTimeGetCurrent and DST

I'm developing an app for iPhone that uploads pictures to a webserver. These pictures have the time of when they were taken in the filename. Since they can be taken from anywhere in the World, I have to keep attention to timezones and DST. I thought I can use CFAbsoluteTimeGetCurrent, that shouldn't be localized ([NSDate date] returns the localized version of time, with or without "AM", "PM", "p.m." or any other variant... for example it returns Arabian characters, if your phone is set in arabic language!).
So, can you suggest me a function to convert CFAbsoluteTimeGetCurrent to a "MySQL like" date, something like "2011-11-03 14:12:10"?
My second question is: what about timezones and daylight saving? CFAbsoluteTimeGetCurrent always returns an UTC date, no matter how the iPhone timezone is set? Is it always DST-free?
Of course I know that the local iPhone date/time could be wrong, but milliseconds-precision is not important for my application :)
Thank you in advance!
What on earth do you mean by
([NSDate date] returns the localized version of time, with or without "AM", "PM", "p.m." or any other variant... for example it returns Arabian characters, if your phone is set in arabic language!).
[NSDate date] returns an NSDate* object. I'm assuming what you really mean is the output of -[NSDate description] returns a string localized in the user's current locale, but then the question is, why are you depending on the output of -[NSDate date]? If you need to format a date a certain way, you should use an NSDateFormatter.
CFAbsoluteTime represents a moment in time, independent of time zones or localization settings. If you want to format a date (i.e. an NSDate object) you should look at NSDateFormatter, which lets you specify the exact format and localization (if any) of the output string. (If you need to work at the Core Foundation level, CFDateFormatter does the same thing.)

Can anyone translate this dateformat?

I’m having a problem with date formats.
I need to convert the following string into af date object: 2011-09-19T12:23:51Z
And then convert the date object back to a string with this format: 19. september 2011
I can’t figure out what the “T” and “Z” is all about though?
Can anyone help me?
Kind regards
Jesper
The "T" is to separate the date from the time.
The "Z" shows that this is in UTC.
This is a standard (extended) ISO-8601 format date/time string - it should be easy to parse with whatever libraries iOS provides.

Returned Date as a string timestamp (Unix?) not sure

I'm working with some videogame server data. The server returns a dictionary with past game details. One of the fields is for the date. That returned object is a string like this:
/Date(1286749014000-0700)/
I'm not exactly sure how that string translates into the date, but it should represent Sunday, October 10, 2010, 3:16 PM.
Is this a Unix timestamp? Do they usually have a suffix like -0700?
Thank you
The number 1286749014 stands for 10 october 2010 5:16:54 pm. So if you substract the 700 from it you should get the right date and time.
Check out the Wikipedia article on Unix time for more information on how it's made up.
The first part ("128674901") exactly represents "Sun, 10 Oct 2010 22:16:54 GMT" date.
In objective-c you can use something like this:
NSTimeInterval unixDate = 128674901;
NSDate *date = [NSDate dateWithTimeIntervalSince1970:unixDate];
It looks like you have a high resolution time with timezone offset.
The "0700" suffix is a time zone.
Which means UTC -07