convert json utc "/Date(1420095600000-0700)/" date to nsdate - objective-c

I get date like "/Data(xxx....xxx-xxxx)" from api. How can i get nsdate form this format. Temporarily I solved this problem to truncate the string and get seconds. but I need proper solution for this problem. kindly help me

Use NSDateFormater to transform this String (the parts within the brackets) into an NSDate and vice versa. See the docs here: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/#//apple_ref/occ/instp/NSDateFormatter/dateFormat and here about the various formating possibilities: https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/DataFormatting/DataFormatting.html#//apple_ref/doc/uid/10000029i
Well, you may have to get rid of the milliseconds.

Related

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.

iPad - Update date format in a string by removing century

I currently have an NSString that contains 03/23/2011.
I would like to check to see if the string matches the XX/XX/XXXX format, and if so, remove the century from the year, ending up with XX/XX/XX, so in the example case, 03/23/11.
Should I do this by trying to get an NSDate from the string and setting the format that I want to get it back, or is there a simpler way?
I would take a look at the NSDateFormatter, I think it holds the methods that you require.
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html#//apple_ref/occ/cl/NSDateFormatter

Parsing Datetime

I have a date time as a string, eg. "2010-08-02", I'm trying to convert it to UTC with the following code snippet
DateTime.ParseExact("2010-08-02Z", "yyyy-MM-ddZ", CultureInfo.InvariantCulture)
When I print to the console, I get the following: 8/1/2010 5:00:00 PM.
Is there a reason why the date shows up as the date before the date I'm trying to parse? I could just add a day to this to advance to the original day, but I wanted to see if there's anything I'm doing wrong in the formatting that's causing this.
EDIT: I had a mixture of being correct and not :)
It's showing you the local time represented by the UTC string. It's annoying that DateTime doesn't make this sort of thing clear, IMO. Additionally, I don't think you want to use 'Z' as the format specifier for the time zone; that's not actually a valid format specifier; it should be 'z', - but that's meant for things like "+01:00". I think you should be using 'K'. Frankly it's not clear, but if you use 'K' it round-trips correctly, certainly ('Z' roundtrips too, but only because it ignores it, treating it as plain text).
You can fix it by just calling ToUniversalTime, or (preferred IMO) specifying DateTimeStyles.AdjustToUniversal as an extra argument:
DateTime dt = DateTime.ParseExact("2010-08-02Z", "yyyy-MM-ddK",
CultureInfo.InvariantCulture,
DateTimeStyles.AdjustToUniversal);
The UTC of midnight for 2010-08-02 happens to be at 5pm on 2010-08-01.
If the original string is just a date in the format "2010-08-02" (without the Z), then why not just:
DateTime.SpecifyKind(
DateTime.ParseExact("2010-08-02",
"yyyy-MM-dd",
CultureInfo.InvariantCulture),
DateTimeKind.Utc);
ParseExact will presumably return a DateTime with Kind = Unspecified, and you can make it UTC or Local as you wish using SpecifyKind.