how to get the format 2012-11-19T19:35:00.0000000-07:00 from NSDate? - objective-c

I have a format 2012-11-19T19:35:00.0000000-07:00 how can I acheive this from NSDate?
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyy-MM-dd'T'HH:mm:ssZ"];
NSDate *today = [NSDate date];
NSString *dateString =[dateFormatter stringFromDate:today];
NSLog(#"Date is: %#",dateString);
I get the NSLog as 2012-11-09T14:22:00+0530
But I want 2012-11-09T19:35:00.0000000-07:00
How can I get this out.Please help me out.

Use
[dateFormatter setDateFormat:#"yyy-MM-dd'T'HH:mm:ss.SSSSSSSZZZZZ"];
More on how to format dates with NSFormater here.
Hope that helps.

NSDate conforms to Unicode Technical Standard, you'll need to look into Date_Format_Patterns to find out the format you want,

According to your requirement, you can do this one. The result will be as
//Your desired : 2012-11-09T19:35:00.0000000-07:00
//You will get : 2012-11-09T12:30:00.0000000+05:30
[dateFormatter setDateFormat:#"yyy-MM-dd'T'HH:mm:ss.SSSSSSSZZZZZ"];

Related

Converting timestamp into NSDate

I have a date in timestamp which looks something like this: 1474914600000
Now, I want to covert this timestamp to NSDate in format of dd-mm-yyyy.
How can this be done in objective c?
You need to convert your timestamp to NSDate and then get NSDate in your desired format. Also your timestamp seems to be in millisecond so you will have to divide it be 1000. You can use below code:
double timeStamp = 1474914600000;
NSTimeInterval timeInterval=timeStamp/1000;
NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeInterval];
NSDateFormatter *dateformatter=[[NSDateFormatter alloc]init];
[dateformatter setDateFormat:#"dd-MM-yyyy"];
NSString *dateString=[dateformatter stringFromDate:date];
dateString value as an output will be: "27-09-2016"
Hope it helps.
To elaborate on balkaran's answer incase you're new to the iOS world. The timestamp you provided seems to go down to milliseconds which you wouldn't need for day times that's why he's dividing by 1000. You would use the dateformatter as follows to return an NSString you can use with the formatted date.
NSDate *date = [NSDate dateWithTimeIntervalSince1970:1474914600000];
NSDateFormatter *formatter = [NSDateFormatter new];
formatter.dateFormat = #"dd-MM-yyyy";
NSString *formattedDate = [formatter stringFromDate:date];
use this:
NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeStamp/1000];
then convert your date in the format you want.

Convert ISO 8601 to NSDate

I have a timestamp coming from server that looks like this:
2013-04-18T08:49:58.157+0000
I've tried removing the colons, I've tried all of these:
Converting an ISO 8601 timestamp into an NSDate: How does one deal with the UTC time offset?
Why NSDateFormatter can not parse date from ISO 8601 format
Here is where I am at:
+ (NSDate *)dateUsingStringFromAPI:(NSString *)dateString {
NSDateFormatter *dateFormatter;
dateFormatter = [[NSDateFormatter alloc] init];
//#"yyyy-MM-dd'T'HH:mm:ss'Z'" - doesn't work
//#"yyyy-MM-dd'T'HH:mm:ssZZZ" - doesn't work
//#"yyyy-MM-dd'T'HH:mm:sss" - doesn't work
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
// NSDateFormatter does not like ISO 8601 so strip the milliseconds and timezone
dateString = [dateString substringWithRange:NSMakeRange(0, [dateString length]-5)];
return [dateFormatter dateFromString:dateString];
}
One of my biggest questions is, is the date format I have above really ISO 8601? All the examples I have seen from people the formats of each are slightly different. Some have ...157-0000, others don't have anything at the end.
This works for me:
NSString *dateString = #"2013-04-18T08:49:58.157+0000";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
// Always use this locale when parsing fixed format date strings
NSLocale *posix = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
[formatter setLocale:posix];
NSDate *date = [formatter dateFromString:dateString];
NSLog(#"date = %#", date);
There is New API from Apple! NSISO8601DateFormatter
NSString *dateSTR = #"2005-06-27T21:00:00Z";
NSISO8601DateFormatter *formatter = [[NSISO8601DateFormatter alloc] init];
NSDate *date = [formatter dateFromString:dateSTR];
NSLog(#"%#", date);
I also have the native API, which is way cleaner... This is the implementation I got in my DateTimeManager class:
+ (NSDate *)getDateFromISO8601:(NSString *)strDate{
NSISO8601DateFormatter *formatter = [[NSISO8601DateFormatter alloc] init];
NSDate *date = [formatter dateFromString: strDate];
return date;
}
Just copy and paste the method, it would do the trick. Enjoy it!
The perfect and best solution that worked for me is:
let isoFormatter = ISO8601DateFormatter();
isoFormatter.formatOptions = [ISO8601DateFormatter.Options.withColonSeparatorInTime,
ISO8601DateFormatter.Options.withFractionalSeconds,
ISO8601DateFormatter.Options.withFullDate,
ISO8601DateFormatter.Options.withFullTime,
ISO8601DateFormatter.Options.withTimeZone]
let date = isoFormatter.date(from: dateStr);
For further more detail, you can refer to apple's official documentation: https://developer.apple.com/documentation/foundation/nsiso8601dateformatter

Problems with Creating an NSDate object from a ISO 8601 string

I'm having problems converting a string into a NSDate object and I'm not sure why.
NSString* tester= #"2012-11-26T10:20:40.187";
NSLog(#"", tester); //Print the Date String
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];
NSDate *date = [formatter dateFromString:tester]; //Store the date
NSDateFormatter *output = [[NSDateFormatter alloc] init];
[output setDateStyle:NSDateFormatterMediumStyle];
NSLog(#"DATE OBJECT:%#\nDATE STRING: \"%#\"", [output stringFromDate:date]); //Print the NSDate object and the string
But all I seem to get is:
DATE OBJECT:(null)
DATE STRING: "2012-11-26T10:20:40.187"
I figure it has something to do with the .187 but I'm not sure. I'm sorry if this is a duplicate but I couldn't figure this out.
Thank you very much in advance!
The abbrevation for milliseconds is "S"not Z which is TimeZone.
It seems that you have read the correct document where that example is from, but
you missed the link to the Unicode Technical Standard #35.
See NsDateFormatter Docu, search for "Formatters in Mac OS X v10.4 use version tr35-4."
try that below:
[formatter setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss.SSS"];
See also NSDateFormatter Question

How to convert an NString to an NSDate and then set the date to a UILabel

I need to convert the following string into a better readable format:
NSString *deadlineFromTable = #"2012-11-13T22:59:00.000Z";
I would like to convert this into an NSDate, so I can format it.
I tried the following, but I get an incompatible pointer error assigning NSString to NSDate when I try to set it to a UILabel (the last line):
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSDate *dateFromString = [dateFormatter dateFromString:deadlineFromTable];
self.deadlineLbl.text = dateFromString;
Thanks for any help.
You need to use the dateFormatter twice. Once for parsing, and once for formatting your string.
You cannot assign a date as a label text directly.
Set format: yyyy-MM-dd'T'HH:mm:ss.Z and then use
NSDate *date = [dateFormatter dateFromString:dedlineFromTable];
Set format: dd-MM-yyyy and then use
NSString *text = [dateFormatter stringFromDate: date];
You need two dateFormatters.
One to convert from your input string to a date and then one to convert from that date into the Label format you want.
You also need to change the format of the date formatter so it matches your string...
NSDateFormatter *dateStringParser = [[NSDateFormatter alloc] init];
[dateStringParser setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.Z"];
NSDate *date = [dateStringParser dateFromString:deadlineFromTable];
NSDateFormatter *labelFormatter = [[NSDateFormatter alloc] init];
[labelFormatter setDateFormat:#"dd-MM-yyyy"];
self.deadlineLbl.text = [labelFormatter stringFromDate:date];
That should do it.

How to turn a NSString into NSDate?

Ive been racking my brains with no luck. Could someone please tell me how i would convert this string:
"2011-01-13T17:00:00+11:00"
into a NSDate?
The unicode date format doc is here
Also, for your situation, you could try this:
// original string
NSString *str = [NSString stringWithFormat:#"2011-01-13T17:00:00+11:00"];
// convert to date
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
// ignore +11 and use timezone name instead of seconds from gmt
[dateFormat setDateFormat:#"YYYY-MM-dd'T'HH:mm:ss'+11:00'"];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:#"Australia/Melbourne"]];
NSDate *dte = [dateFormat dateFromString:str];
NSLog(#"Date: %#", dte);
// back to string
NSDateFormatter *dateFormat2 = [[NSDateFormatter alloc] init];
[dateFormat2 setDateFormat:#"YYYY-MM-dd'T'HH:mm:ssZZZ"];
[dateFormat2 setTimeZone:[NSTimeZone timeZoneWithName:#"Australia/Melbourne"]];
NSString *dateString = [dateFormat2 stringFromDate:dte];
NSLog(#"DateString: %#", dateString);
[dateFormat release];
[dateFormat2 release];
Hope this helps.
put the T part in single quotes, and check the unicode docs for the exact formatting. In my case, I have something similar, which I do this:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"YYYY-MM-dd'T'HH:mm:ss.SSS"];
Again, not exactly the same, but you get the idea. Also, be careful of the timezones when converting back and forth between strings and nsdates.
Again, in my case, I use:
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:#"America/New_York"]];
Did you try this
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSDate *dateT = [dateFormatter dateFromString:str];
Cheers
You might check out TouchTime.
https://github.com/jheising/TouchTime.
It's a direct port of the awesome strtotime function in PHP in 5.4 for Cocoa and iOS. It will take in pretty much any arbitrary format of date or time string and convert it to an NSDate.
Hope it works, and enjoy!
Try using this cocoapods enabled project. There are many added functions that will probably be needed as well.
"A category to extend Cocoa's NSDate class with some convenience functions."
https://github.com/billymeltdown/nsdate-helper
Here's an example from their page:
NSDate *date = [NSDate dateFromString:#"2009-03-01 12:15:23"];