I am trying to do a date from string however it always makes the date in the month of January... Why?
Code:
NSMutableArray *dateArray = [NSMutableArray array];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"YYYYMMDD"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
for (id <NSFetchedResultsSectionInfo> sectionInfo in [fetchedResultsController sections]) {
NSLog(#"Adding date: %#", [sectionInfo name]);
NSDate *newDate = [dateFormatter dateFromString:[sectionInfo name]];
NSLog(#"Adding date 2: %#", newDate);
[dateArray addObject:newDate];
}
LOGS:
2012-03-01 15:14:48.124 MyApp[21793:fb03] Adding date: 20120827
2012-03-01 15:14:48.124 MyApp[21793:fb03] Adding date 2: 2012-01-27 00:00:00 +0000
2012-03-01 15:14:48.125 MyApp[21793:fb03] Adding date: 20120830
2012-03-01 15:14:48.125 MyApp[21793:fb03] Adding date 2: 2012-01-30 00:00:00 +0000
2012-03-01 15:14:48.125 MyApp[21793:fb03] Adding date: 20120831
2012-03-01 15:14:48.126 MyApp[21793:fb03] Adding date 2: 2012-01-31 00:00:00 +0000
2012-03-01 15:14:48.126 MyApp[21793:fb03] Adding date: 20120906
2012-03-01 15:14:48.127 MyApp[21793:fb03] Adding date 2: 2012-01-06 00:00:00 +0000
2012-03-01 15:14:48.127 MyApp[21793:fb03] Adding date: 20120907
2012-03-01 15:14:48.128 MyApp[21793:fb03] Adding date 2: 2012-01-07 00:00:00 +0000
2012-03-01 15:14:48.128 MyApp[21793:fb03] Adding date: 20120910
2012-03-01 15:14:48.128 MyApp[21793:fb03] Adding date 2: 2012-01-10 00:00:00 +0000
2012-03-01 15:14:48.129 MyApp[21793:fb03] Adding date: 20120913
2012-03-01 15:14:48.129 MyApp[21793:fb03] Adding date 2: 2012-01-13 00:00:00 +0000
The day of month specifier is dd, not DD. Also, you might want to use the yyyy format for years, as YYYY means the ISO 'Week of Year', and may be different from the actual year. So your final format should look like:
[dateFormatter setDateFormat:#"yyyyMMdd"];
Related
For some reason, this code is not converting the second string correctly; this is the code:
NSLog(#"\n\nsearchStartTime: %# searchEndTime: %#",searchStartTime, searchEndTime);
[dateFormatter setDateFormat: #"yyyy-MM-dd HH:mm a"];
startDate = [dateFormatter dateFromString:searchStartTime];
endDate = [dateFormatter dateFromString:searchEndTime];
NSLog(#"\n\nstartDate: %# endDate: %#", startDate, endDate);
This the definition and value of the searchStartTime and searchEndTime:
searchStartTime: 2015-03-05 08:30 AM searchEndTime: 2015-03-05 09:30 AM
This is the result from NSLog:
startDate: 2015-03-05 08:30:00 +0000 endDate: 2015-03-05 08:30:00 +0000
What's wrong with my code here? I want to take these two date/time objects and use them in a NSPredicate to compare dates in a Core Data record.
It all comes down to knowing your format code strings. You are misusing "HH" here so the hour is coming out wrong.
Another problem is that you are not capturing the error. By calling dateFromString:, you are missing out on your chance to hear about errors. Use getObjectValue:forString:range:error: to learn of problems.
Anyway, I substituted "hh" where you have "HH" and the answers came out right:
NSString* searchStartTime = #"2015-03-05 08:30 AM";
NSString* searchEndTime = #"2015-03-05 09:30 AM";
NSLog(#"\n\nsearchStartTime: %# searchEndTime: %#",searchStartTime, searchEndTime);
NSDateFormatter* dateFormatter = [NSDateFormatter new];
[dateFormatter setDateFormat: #"yyyy-MM-dd hh:mm a"];
NSDate* startDate = [dateFormatter dateFromString:searchStartTime];
NSDate* endDate = [dateFormatter dateFromString:searchEndTime];
NSLog(#"\n\nstartDate: %# endDate: %#", startDate, endDate);
startDate: 2015-03-05 16:30:00 +0000 endDate: 2015-03-05 17:30:00 +0000
I am in California, and the time is shown relative to London, so if you subtract the time difference you'll see that this is correct.
One error is "HH" which is 24-hour tine but you are also using "a" for an & pm.
Instead use "hh" for 12-hour time.
Example:
NSString *searchStartTime = #"2015-03-05 08:30 AM";
NSString *searchEndTime = #"2015-03-05 09:30 AM";
NSLog(#"\n\nsearchStartTime: %# searchEndTime: %#", searchStartTime, searchEndTime);
NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormatter setDateFormat: #"yyyy-MM-dd hh:mm a"];
NSDate *startDate = [dateFormatter dateFromString:searchStartTime];
NSDate *endDate = [dateFormatter dateFromString:searchEndTime];
NSLog(#"\n\nstartDate: %# endDate: %#", startDate, endDate);
Output:
searchStartTime: 2015-03-05 08:30 AM searchEndTime: 2015-03-05 09:30 AM
startDate: 2015-03-05 13:30:00 +0000 endDate: 2015-03-05 14:30:00 +0000
See: ICU Formatting Dates and Times
Also: Date Field SymbolTable.
I want to get all NSDate from to to 30 days later. However it doesn't work correctly.
Here is the code i am using:
for (int i = 0; i < days; i++) {
NSDate *today = [self getToday];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
components.day = i;
NSDate *nextMonth = [gregorian dateByAddingComponents:components toDate:today options:0];
NSDateComponents *nextMonthComponents = [gregorian components:NSYearCalendarUnit | NSMonthCalendarUnit fromDate:nextMonth];
NSDateComponents *todayDayComponents = [gregorian components:NSDayCalendarUnit fromDate:today];
nextMonthComponents.day = todayDayComponents.day+i;
NSDate *nextMonthDay = [gregorian dateFromComponents:nextMonthComponents];
DLog(#"nextMonthDay = %#",nextMonthDay);
[months addObject:nextMonthDay];
}
I am getting list :
nextMonthDay = 2013-01-03 22:00:00 +0000
nextMonthDay = 2013-01-04 22:00:00 +0000
nextMonthDay = 2013-01-05 22:00:00 +0000
nextMonthDay = 2013-01-06 22:00:00 +0000
nextMonthDay = 2013-01-07 22:00:00 +0000
nextMonthDay = 2013-01-08 22:00:00 +0000
nextMonthDay = 2013-01-09 22:00:00 +0000
nextMonthDay = 2013-01-10 22:00:00 +0000
nextMonthDay = 2013-01-11 22:00:00 +0000
nextMonthDay = 2013-01-12 22:00:00 +0000
nextMonthDay = 2013-01-13 22:00:00 +0000
nextMonthDay = 2013-01-14 22:00:00 +0000
nextMonthDay = 2013-01-15 22:00:00 +0000
nextMonthDay = 2013-01-16 22:00:00 +0000
nextMonthDay = 2013-01-17 22:00:00 +0000
nextMonthDay = 2013-01-18 22:00:00 +0000
nextMonthDay = 2013-01-19 22:00:00 +0000
nextMonthDay = 2013-01-20 22:00:00 +0000
nextMonthDay = 2013-01-21 22:00:00 +0000
nextMonthDay = 2013-01-22 22:00:00 +0000
nextMonthDay = 2013-01-23 22:00:00 +0000
nextMonthDay = 2013-01-24 22:00:00 +0000
nextMonthDay = 2013-01-25 22:00:00 +0000
nextMonthDay = 2013-01-26 22:00:00 +0000
nextMonthDay = 2013-01-27 22:00:00 +0000
nextMonthDay = 2013-01-28 22:00:00 +0000
nextMonthDay = 2013-01-29 22:00:00 +0000
nextMonthDay = 2013-01-30 22:00:00 +0000
nextMonthDay = 2013-03-03 22:00:00 +0000
nextMonthDay = 2013-03-04 22:00:00 +0000
At the end of the list is:
nextMonthDay = 2013-01-29 22:00:00 +0000
nextMonthDay = 2013-01-30 22:00:00 +0000
nextMonthDay = 2013-03-03 22:00:00 +0000
nextMonthDay = 2013-03-04 22:00:00 +0000
So this is incorrect. There should be instead
2013-01-29 22:00:00 +0000
2013-01-30 22:00:00 +0000
2013-03-01 22:00:00 +0000
2013-03-02 22:00:00 +0000
Maybe someone could help me on this ?
Thank you.
You want to look at the Date & Time Programming Guide, especially: [Adding Components to a Date
](https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DatesAndTimes/Articles/dtCalendricalCalculations.html#//apple_ref/doc/uid/TP40007836-SW1)
You need to use the setDay: method in your loop to generate NSDate instances for each day offset from the reference NSDate you start with.
NSLog(#"30 consecutive days!");// just hello.
// Set up data outside the loop...
NSDate *today = [[NSDate alloc] init];
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
// Surely you want to do something clever with these beyond logging them immediately, right? Let's put them in an array. They'll be in order and we can use them again later.
NSMutableArray *thirtyConsecutiveDays = [[NSMutableArray alloc] initWithCapacity:30];
int days = 30; // This is one less than thirty-one.
// Ok, let's loop!
for (int i = 0; i < days; i++) {
// We're going to want a new one of these each time through the loop.
NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
// We need to tell it how far to offset from the reference NSDate.
[offsetComponents setDay:i]; // incrementing by i days.
// We will have our current NSDateComponents friend tell our NSCalendar friend what NSDate we want next...
NSDate *anotherDate = [gregorian dateByAddingComponents: offsetComponents
toDate:today options:0];
// Now add our latest NSDate iteration to our array.
[thirtyConsecutiveDays addObject:anotherDate];
}
// This is just logging results.
for (NSDate *aDate in thirtyConsecutiveDays) {
NSLog(#"%#",aDate);
}
I have different solution for you
int min = 60*60*24;
for (int i = 0; i < days; i++) {
NSDate *today = [self getToday];
NSDate *newDate = [today dateByAddingTimeInterval:(min*i)];
}
Hope it helps you
How about:
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
components.day = 1;
NSDate *date = [self getToday];
for (int i = 1; i < 30 ; i++) {
date = [calendar dateByAddingComponents:components toDate:date options:0];
[months addObject:date];
}
How do I convert "4:15:00 PM" to an NSDate? Below is the code I have:
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"HH:mm:ss aa"];
NSDate *date = [df dateFromString:#"4:15:00 PM"];
NSLog(#"date: %#",date);//outputs: 1970-01-01 17:15:00 +0000
//should output: 1970-01-01 16:15:00 +0000
UPDATE: I updated based on responses and I am still having the incorrect show:
NSLog(#"date: %#",[df stringFromDate:date]);//outputs: 12:15:00 PM
//should output: 4:15:00 PM
The question is why does the time change from 4:15:00 PM 12:15:00 PM.
Actually, this is correct.
However, the debugger and NSLog display the time in GMT.
If you want to display local time, use a NSDateFormatter and stringFromDate.
Update:
Your date formatter format is also set incorrectly.
It should be: [df setDateFormat:#"hh:mm:ss aa"]; since you are using a 12 hour clock.
This is because of your locale.
NSDateFormatter outputs a NSDate - which is always in GMT time.
If you want your initial string interpreted as GMT time then you need to include the timezone.
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"HH:mm:ss aa z"];
NSDate *date = [df dateFromString:#"4:15:00 PM GMT"];
Hopefully this is just a quick one and I'm missing something simple...
I've got an NSDateFormatter, which I'm using to convert the string 2011-11-10 into a Date object.
NSDateFormatter *fmtDate = [[NSDateFormatter alloc] init];
[fmtDate setDateFormat:#"YYYY-MM-DD"];
// input of 2011-11-10, output of 2011-01-10 00:00:00 +0000
[appointment setDate:[fmtDate dateFromString:
[tempAppointment objectForKey:#"date"]
]];
The return from the NSDateFormatter is stored in a managedObjectContext. My problem is that the dateFormatter is returning the date as 2011-01-10 00:00:00 +0000
Why is it reducing the month from Nobvember to January? It's retaining the year and the day fine, but not the month.
Do I need to include the hours when I store the date? Or is it something to do with the format I set?
Try the format #"yyyy-MM-dd" for the format as specified here in the "Use Format Strings to Specify Custom Formats" section. It has an example and list YYYY as a common mistake.
Your date format string is not correct using YYYY. Try yyyy-M-d or yyyy-MM-dd
NSString *dateString = #"2011-11-10";
NSDateFormatter *fmtDate = [[NSDateFormatter alloc] init];
[fmtDate setDateFormat:#"yyyy-M-d"];
NSDate *date = [fmtDate dateFromString:dateString];
NSLog(#"date: %#", date);
Outputs:
2011-11-10 20:01:40.638 Craplet[81514:707] date: 2011-11-10 05:00:00 +0000
See:
http://unicode.org/reports/tr35/tr35-10.html#Date_Format_Patterns
I'm creating a universal feed reader and i need to format the rss pubDate but the rss pubdate is always different, for example:
Wed, 25 May 2011 02:10:00 CEST
Wed, 25 May 2011 18:54:26 +00:00
Wed, 25 May 2011 08:13:22 +0000
Wed, 25 May 2011 14:21:54 GMT
26 May 2011 10:32:00 +0100
I tried to use this code:
NSString *dateString = #"Wed, 25 May 2011 18:54:26 +00:00";
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
[df setDateFormat:#"EEE, dd MMMM yyyy HH:mm:ss +00:00"];
NSLocale *enLocale = [[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"] autorelease];
[df setLocale:enLocale];
NSDate *date = [df dateFromString:dateString];
NSLog(#"'%#' = %#", dateString, date);
NSDateFormatter* df2 = [[[NSDateFormatter alloc] init] autorelease];
[df2 setDateFormat:#"EEE, dd MMMM yyyy HH:mm:ss"];
NSString *dateString2 = [df2 stringFromDate:date];
This code, however, works only with one type of rss pubDate, how can fix this problem?? how can create a universal dateformatter??
pubDate is in a date format, so you do the same format in your code to reflect that of the pubDate format.
EEE, dd MMMM yyyy HH:mm:ss +00:00 is equal to puDate: Wed, 25 May 2011 18:54:26 +00:00
You just have to compensate for the date format stored in pubDate.