Parsing NSString to NSDate object - nsdate

I am trying to parse the following NSString to an NSDate object:
"Sat, 21 Jan 2012 13:06:00 +0100"
I know that I need the following lines of code to do this:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"EEE, MMM dd yyyy HH:mm:ss GMT"];
NSDate *dateObject = [dateFormat dateFromString:dateString];
But since dateObject always has the value nil, I assume that the expression inside the dateFormatter is not correct.
Does anyone have a suggestion for the right formatter statement?
Have already tried a lot of options, but none of them seems to work.
Thanks a lot.

Your date format is wrong. it should be:
EEE, dd MMM yyyy HH:mm:ss GMT
for
Sat, 21 Jan 2012 13:06:00 +0100
You've accidentally exchanged "dd" and "MMM".

Related

NSDate rolling year over to next year on Dec 31

If I have this date:
dtToUse __NSTaggedDate * 2017-12-31 05:00:00 UTC
which I get from a string using this method:
- (NSDate*) convertStringToDate : (NSString*) strToConvert andTheFormatToUse: (NSString*) strFormat {
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:strFormat];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
return [dateFormatter dateFromString:strToConvert];
}
and I use this code to display it:
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM/dd/YYYY"];
NSString* strDate = [dateFormatter stringFromDate:dtExpiration];
self.txtCurrentField.text = [dateFormatter stringFromDate:dtExpiration];
But no matter what I do, the year increments 1 - so 2017 becomes 2018
This only happens for Dec. 31, any year. I suspect it is a time zone issue but I am not sure how to fix, as I am converting time zone to local already
Just try
[dateFormatter setDateFormat:#"MM/dd/yyyy"];
instead of
[dateFormatter setDateFormat:#"MM/dd/YYYY"];
After that, it will show 12/31/2017.
Different between YYYY and yyyy.
https://stackoverflow.com/a/15133656/1342266

How do you get the seconds since a past date to now?

How can I get the seconds that have passed since 1980-01-01 00:00:00 +1100 using NSTimeInterval?
// I need the function to use something like and am having an issue
NSDate *aDate = (NSDate*)#"1980-01-01 00:00:00 +1100";
NSDate *seconds = [NSDate dateWithTimeInterval:60*60*24 sinceDate:aDate];
NSLog(#"seconds since Jan 1980 %#",seconds);
// I am trying to replace the following
//NSTimeInterval dateinterval = [[NSDate date] timeIntervalSince1970];
NSTimeInterval dateinterval = seconds;
NSDate only retrieves the GMT at +0000 which is not helpful in real world applications. Local dates are mandatory.
Is this too hard or can it not be done this way?
You are initializing your NSDate object wrong. You can't directly cast an NSString as an NSDate, you need to use NSDateFormatter:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"yyyy-MM-dd HH:mm:ss z";
NSDate *aDate = [formatter dateFromString:#"1980-01-01 00:00:00 +1100"];
NSDate *now = [NSDate date];
NSTimeInterval seconds = [now timeIntervalSinceDate:aDate];
To format the date for a different time zone, use a new NSDateFormatter and set its local and timeZone.

NSDateFormatterFullStyle api is giving different results

NSDateFormatterFullStyle api is giving different date formats in iPod and iPhone. In
iPhone : Friday, 21 December 2012 (coma is missed)
iPod : Friday, 21 December, 2012.
Sample code:
NSDate *date = [NSDate dateWithTimeIntervalSince1970:[postdateText doubleValue] / 1000.0];
NSLog(#"date %#:",date);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[NSLocale currentLocale]];
NSString *dateString = [NSDateFormatter localizedStringFromDate:date dateStyle:NSDateFormatterFullStyle timeStyle:NSDateFormatterNoStyle];
NSTimeZone *tz = [NSTimeZone timeZoneWithName:#"CST"];
[dateFormatter setTimeZone:tz];
return dateString;
// I was tried in this way as well
//[dateFormatter setDateFormat:#"EEEE, MMMM dd, YYYY"];
//[dateFormatter setDateStyle:NSDateFormatterFullStyle];
That is changing depends on region format as well as language. Go to settings->General->International->Region Format. It should work.

how to get only date from string?

how to get only date from datetime date value
my code is:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd"];
[dateFormat setDateStyle:NSDateFormatterShortStyle];
lblDateAvailable.text = [dateFormat stringFromDate:[datepick date]];
NSDate *date=[dateFormat dateFromString:lblDateAvailable.text];
NSLog(#"%#",date);
result=2012-08-17 18:30:00 +0000
i want to get only date from string.so plz suggest me.
Convert Date into string and using this method Separated the component and pass the Separated value in your label.
NSArray* components = [yourdatestring componentsSeparatedByString:#" "];

How to Convert XML Date format to Cocoa

I have a Date format:
2009-08-10T16:03:03Z
that I want to convert to:
#"MMM dd, HH:mm a"
I retrieve the xml format in an NSString. I tried to use the NSDateformatter:
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"MMM dd, HH:mm a"];
any ideas?
NSString *yourXMLDate = #"2009-08-10T16:03:03Z"
NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
[inputFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];
NSDate *inputDate = [inputFormatter dateFromString:yourXMLDate];
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:#"MMM dd, HH:mm a"];
NSString *outputDate = [outputFormatter stringFromDate:inputDate];
Hope this helps!
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"E, d LLL yyyy HH:mm:ss Z"]; // Thu, 18 Jun 2010 04:48:09 -0700
NSDate *date = [dateFormatter dateFromString:self.currentDate];
[item setObject:date forKey:#"date"];
[items addObject:[item copy]];
here edit the following code
[dateFormatter setDateFormat:#"E, d LLL yyyy HH:mm:ss Z"]; // Thu, 18 Jun 2010 04:48:09 -0700
to your required Format.

Resources