NSDateFormatterFullStyle api is giving different results - nsdate

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.

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.

Formatting Date from server time to local time using NSTimeZone and Dateformatter

I am receiving a date/time as a NSString from my server where I am converting that time into a NSDate to the users local time using NSTimeZone. After which I try to reformat this NSDate into a better more readable NSString using the new NSDateFormatter format, however when I try to apply this new format it reverts the resulting dateString back to the original Server time.
I would like to know what I am doing wrong, I would like to show the converted time in the new format.
this is the code I am using
// set date format
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"yyyy-MM-dd HH:mm:ss";
// change time to systemTimeZone
NSTimeZone *timeZone = [NSTimeZone systemTimeZone];
[dateFormatter setTimeZone:timeZone];
NSDate *localTime = [dateFormatter dateFromString:[singleInstanceActivationHistoryDictionay objectForKey:#"ActivationTime"]];
// reformat converted Time to readable format
NSDateFormatter *dateFormat1 = [[NSDateFormatter alloc] init];
[dateFormat1 setDateFormat:#"dd/MM/yy - hh:mm a"];
NSString *dateWithNewFormat = [dateFormat1 stringFromDate:localTime];
NSLog(#"TimeZone - %#", timeZone);
NSLog(#"UTC ServerTime - %#", [singleInstanceActivationHistoryDictionay objectForKey:#"ActivationTime"]);
NSLog(#"UTC to deviceTimeZone - %#", localTime);
NSLog(#"NewFormat - %#", dateWithNewFormat);
This is an example of my output
TimeZone - Pacific/Auckland (NZST) offset 43200
UTC ServerTime - 2013-08-22 01:45:59
UTC to deviceTimeZone - 2013-08-21 13:45:59 +0000
NewFormat - 22/08/13 - 01:45 AM
any help would be greatly appreciated
The NSDateFormatter that reads the date must be set to the timezone that the date you are parsing is in, in your case, it is UTC. The date formatter will then be able to produce an NSDate object (which represents a specific moment in time regardless of timezones). You can then give that NSDate object to another NSDateFormatter that is configured to format dates in a specific time zone.
// set date format
NSDateFormatter *dateParser = [[NSDateFormatter alloc] init];
dateParser.dateFormat = #"yyyy-MM-dd HH:mm:ss";
dateParser.timeZone = [NSTimeZone timeZoneWithName:#"UTC"];
NSDate *specificMomentInTime = [dateParser dateFromString:[singleInstanceActivationHistoryDictionay objectForKey:#"ActivationTime"]];
// reformat converted Time to readable format
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"dd/MM/yy - hh:mm a";
dateFormatter.timeZone = [NSTimeZone defaultTimeZone];
NSString *dateWithNewFormat = [dateFormatter stringFromDate:specificMomentInTime];
NSLog(#"UTC ServerTime - %#", specificMomentInTime);
NSLog(#"NewFormat - %#", dateWithNewFormat);

NSDateFormatter reports June 2, 2013 as being in week zero

I am using NSDateFormatter to convert a series of dates to a week number within a month.
The date formatting code I am using is yyyyMMW and everything I have read tells me that W will be between 1-5.
But, the 2nd of June 2013 fell on a Sunday (the default start day of the week in the gregorian calendar) and it's week number is reported as 0 even though the start date of the week is calculated correctly:
2013-06-03 14:15:45.611 date=20130531, week=2013055, start of week=20130526
2013-06-03 14:15:45.612 date=20130602, week=2013060, start of week=20130602
2013-06-03 14:15:45.612 date=20130603, week=2013061, start of week=20130602
Some quick and dirty test code to reproduce the log shown above:
NSDateFormatter *dateFormatDaily = [[NSDateFormatter alloc] init];
[dateFormatDaily setDateFormat:#"yyyyMMdd"];
NSDateFormatter *dateFormatterWeekly = [[NSDateFormatter alloc] init];
[dateFormatterWeekly setDateFormat:#"yyyyMMW"];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[calendar setFirstWeekday:1]; // default but set here for clarity
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
[dateComponents setMonth:5];
[dateComponents setDay:31];
[dateComponents setYear:2013];
NSDate *date_1 = [calendar dateFromComponents:dateComponents];
[dateComponents setMonth:6];
[dateComponents setDay:2];
NSDate *date_2 = [calendar dateFromComponents:dateComponents];
[dateComponents setDay:3];
NSDate *date_3 = [calendar dateFromComponents:dateComponents];
NSArray *datesToTest = #[date_1, date_2, date_3];
for (NSDate *date in datesToTest) {
NSString *weekNo = [dateFormatterWeekly stringFromDate:date];
NSDate *beginningOfWeek = nil;
BOOL rc = [calendar rangeOfUnit:NSWeekCalendarUnit startDate:&beginningOfWeek interval:NULL forDate:date];
if (rc) {
NSLog(#"date=%#, week=%#, start of week=%#", [dateFormatDaily stringFromDate:date], weekNo, [dateFormatDaily stringFromDate:beginningOfWeek]);
} else {
NSLog(#"Could not calculate beginning of week");
}
}
Any ideas? A week number of 0 under any circumstances seems wrong to me.
Thanks
There are various parameters that cause this effect. First of all, you did not set a calendar for the date formatter. If you add
[dateFormatterWeekly setCalendar:calendar];
to your code, then the output will be as you expected:
date=20130531, week=2013055, start of week=20130526
date=20130602, week=2013062, start of week=20130602
date=20130603, week=2013062, start of week=20130602
But in your case, the date formatter uses the current calendar, and therefore has separate parameters firstWeekDay and minimumDaysInFirstWeek. These parameters are locale dependent. If I test this on the iOS Simulator with the "Region Format" set to "German -> Germany", then
[[dateFormatterWeekly calendar] firstWeekday] = 2
[[dateFormatterWeekly calendar] minimumDaysInFirstWeek] = 4
and I assume that you will have similar values, because now I get the same output as you.
Now for the date formatter, the week starts on a Monday, which means that June 2 is in the week starting at May 27. This counts as "week #0" in June, because only one day of this week is in June, but minimumDaysInFirstWeek = 4. The first week in a month that has at least
minimumDaysInFirstWeek days, counts as "week #1".
(I found the relevance of the minimumDaysInFirstWeek parameter here:
http://www.cocoabuilder.com/archive/cocoa/326845-week-of-month-confusion.html)

dateFromString return nil after change 24 hour

NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setDateFormat:#"hh:mm a"];
NSDate* sourceDate = [timeFormat dateFromString:#"19/11/2010 12:00 am"];
if(sourceDate==nil)
{
NSLog(#"source date nil");
}
I'm using 24 hour setting in iPhone. After I change to 24 hour setting, datefromstring always return nil. I rechange to 12 hour format, it's working again. Why ?
The locale needs to be set to avoid being affected by the 24-hour setting change.
The format also has to match the input string (which in your example includes the date):
NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
NSLocale *locale = [[[NSLocale alloc]
initWithLocaleIdentifier:#"en_US_POSIX"] autorelease];
[timeFormat setLocale:locale];
[timeFormat setDateFormat:#"dd/MM/yyyy hh:mm a"];
NSDate* sourceDate = [timeFormat dateFromString:#"19/11/2010 12:00 am"];
if(sourceDate==nil)
{
NSLog(#"source date nil");
}
See QA1480 for more information.

Resources