Change NSDateFormatter For Dates in Array? - iphone

Right now my dates look like this:`2011-01-01 5:45:23 +0000
I want them to look like this: 2011-01-01 00:00:00 +0000 (notice how everything is 00 beside the date, month, and year).
I have an array of NSdates which is fetched from this code:
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Session" inManagedObjectContext:self.managedObjectContext];
[request setEntity:entity];
[request setResultType:NSDictionaryResultType];
[request setReturnsDistinctResults:YES]; //set to YES if you only want unique values of the property
[request setPropertiesToFetch :[NSArray arrayWithObject:#"timeStamp"]]; //name(s) of properties you want to fetch
NSError *error;
NSArray *objects = [managedObjectContext executeFetchRequest:request error:&error];
NSArray *data = [objects valueForKey:#"timeStamp"];
NSLog(#"The content of data is%#", data);
I want to remove daylight savings and have 0:00:00 for the time aspect of each date, thus I need this NSDateFormatter method:
NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:#"YYYY-MM-dd HH:mm:ss"]; // e.g., set for mysql date strings
[formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT"]];
NSString* mysqlGMTString = [formatter stringFromDate:[NSDate date]];
So how can I combine the two to work together?

NSDateFormatter *formatter = [[[NSDateFormatter alloc] init];
[formatter setDateFormat:#"YYYY-MM-dd HH:mm:ss"];
[formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT"]];
for (NSDate *d in data) {
NSString* mysqlGMTString = [formatter stringFromDate:d];
// ...
}
[formatter release];

Since your requirement is to have strings in the data array, this code should suit your purpose.
NSMutableArray * resultsArray = [NSMutableArray array];
NSCalendar * gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
[gregorian setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT"]];
NSUInteger flags = ( NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit );
NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];
[formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT"]];
[datesArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSDate * date = [gregorian dateFromComponents:[gregorian components:flags fromDate:obj]];
[resultsArray addObject:[formatter stringFromDate:date]];
}];
resultsArray will contain the strings that you need. Now you can have data referring to the object
As for the search with a date part,
- (void)calendarMonthView:(TKCalendarMonthView *)monthView didSelectDate:(NSDate *)d {
// Assuming `data` is from the code above & the date is in `00:00:00 +0000` form.
NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];
[formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT"]];
NSString * dateString = [formatter stringFromDate:d];
if ( [data containsObject:dateString] ) {
// Push object to detail view??
}
}
Also use yyyy instead of YYYY. They are not the same.

Related

How to convert the NSString of format (2013-07-25T06:15:33.180-04:00) to NSDate?

How to convert the NSString of format (2013-07-25T06:15:33.180-04:00) to NSDate?
I am trying to convert using the following code
NSDateFormatter *rfc3339TimestampFormatterWithTimeZone = [[NSDateFormatter alloc] init];
[rfc3339TimestampFormatterWithTimeZone setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"]];
[rfc3339TimestampFormatterWithTimeZone setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss ZZZ"];
NSDate *theDate = nil;
NSError *error = nil;
if (![rfc3339TimestampFormatterWithTimeZone getObjectValue:&theDate forString:dateString range:nil error:&error]) {
NSLog(#"Date '%#' could not be parsed: %#", dateString, error);
}
NSString *strDate = [rfc3339TimestampFormatterWithTimeZone stringFromDate:theDate];
return strDate;
Please help me
try like this,i hope you'l succeed..
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"];
NSDate *date = [dateFormatter dateFromString:#"2013-07-25T06:15:33.180-04:00"];//chnage symbal here.
NSLog(#"%#",date);

Datetime to date only in objective c

i have a date time like this
2012-01-01 10:00:00
i need to convert it into date only like this
2012-01-01
Can anyone help me please
this is my code
NSDate *adate = objIStruct_Conference.m_DtDate;
NSLog(#"DATE========%#",objIStruct_Conference.m_DtDate);
NSString *DateString = [NSString stringWithFormat:#"%#", adate ];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"] autorelease]];
[formatter setDateFormat:#"yyyy:MM:dd"];
NSLog(#"NEW DATE====%#", [formatter stringFromDate:adate]);
[formatter release];
i am getting DATE===== 2012-01-01 10:00:00 , NEW DATE=====(null)
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy:MM:dd"];
NSLog(#"%#", [formatter stringFromDate:[NSDate date]]);
[formatter release];
See the Class Reference of NSDateFormatter
Please use this code:
NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init] autorelease];
[dateFormat setDateFormat:#"yyyy-MM-dd"];
NSLog(#"%#",[dateFormat stringFromDate:date]);
it seems your adate pointer would be nil, because it is working perfectly for me on the real device with a valid NSDate but when I set the _date to nil, the output will be nil as in your case.
NSDate *_date = [NSDate date];
NSDateFormatter *_dateFormatter = [[NSDateFormatter alloc] init];
[_dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"]];
[_dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSLog(#"%#", [_dateFormatter stringFromDate:_date]);
the output is:
2012-08-08

EKCalender creates calendar but can't see it in iCal

I am trying to create a new calendar in iCal. Now I am able to create a new Calendar with some name. But my problem is when I try to create to Local calendar I cant see it in calendar list. Any one have any idea about this? I have tried using all types of sources but not able to find exact solution for this.
Thank you in advance.
NSMutableArray *arrAlarm = [[NSMutableArray alloc]init];
EKAlarm *alarm1 = [EKAlarm alarmWithRelativeOffset:3600];
EKAlarm *alarm2 = [EKAlarm alarmWithRelativeOffset:-3600]; //86400 1 day
[arrAlarm addObject:alarm1];
[arrAlarm addObject:alarm2];
EKEventStore *eventStore = [[EKEventStore alloc] init];
EKEvent *ekevent = [EKEvent eventWithEventStore:eventStore];
ekevent.title = strname;
NSLog(#"%#",ekevent.startDate);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE MMM d,yyyy"];
NSDate *date = [dateFormatter dateFromString:strdate];
// Convert to new Date Format
[dateFormatter setDateFormat:#"YYYY-MM-DD HH:MM:SS ±HHMM"];
NSString *newDate = [dateFormatter stringFromDate:date];
// dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
// [dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"] autorelease]];
[dateFormatter setDateFormat:#"hh:mm a"];
NSDate* firstDate = [dateFormatter dateFromString:time];
NSDate* secondDate = [dateFormatter dateFromString:endtime];
NSTimeInterval timeDifference = [secondDate timeIntervalSinceDate:firstDate];
// Convert to new Date Format
[dateFormatter setDateFormat:#"HH:MM"];
NSString *t11 = [dateFormatter stringFromDate:firstDate];
NSString *t12 = [dateFormatter stringFromDate:secondDate];
NSDate *dt1 = [dateFormatter dateFromString:t11];
NSDate *dt2 = [dateFormatter dateFromString:t12];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"hh"];
NSString *hourstring = [NSString stringWithFormat:#"%#",
[df stringFromDate:dt1]];
[df setDateFormat:#"mm"];
NSString *minstring = [NSString stringWithFormat:#"%#",
[df stringFromDate:dt1]];
[df release];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
NSDateComponents *components = [gregorian components: NSUIntegerMax fromDate: date];
[components setHour: [hourstring intValue]];
[components setMinute: [minstring intValue]];
[components setSecond: 00];
// NSDate *newDate = [gregorian dateFromComponents: components];
[gregorian release];
ekevent.startDate = date;
ekevent.endDate = [[NSDate alloc] initWithTimeInterval:timeDifference sinceDate:date];
[ekevent setCalendar:[eventStore defaultCalendarForNewEvents]];
ekevent.alarms = arrAlarm;
[arrAlarm release];
NSError *err;
[eventStore saveEvent:ekevent span:EKSpanThisEvent error:&err];
Try this code you will get the desired result.

convert string into NSDate

i have a "DateTimeArray" this array contain
DateTimeArray[index 0] = 20.05.2011 12:12:50
DateTimeArray[index 1]= 20.05.2011 12:13:20
DateTimeArray[index 2]= 20.05.2011 12:20:10
all the value are in string,and i want to convert this string into NSDate and only want to access time not date
and then this time values will be stored in array and this newly array will be used for drawing line chart
i hope some one know this issue
thank you very much
Code as follows,
for(int index=0;index<[DateTimeArray count];index++)
{
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"HH:mm:ss"];
NSString *timeString=[[[DateTimeArray objectAtIndex:index]componentsSeparatedByString:#" "]objectAtIndex:1];
NSDate *time=[df dateFromString:timeString];
[timeArray addObject:time];
[df release];
}
Try this
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"HH:mm:ss"];
NSDate *formattedDate = [df dateFromString:[DateTimeArray objectAtIndex:requiredIndex]];
[df release];
Best and cleaner approach:
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"dd.MM.yyyy HH:mm:ss"];
for(int i=0; i < [DateTimeArray count]; ++i)
{
NSString *string = [DateTimeArray objectAtIndex:i];
NSDate *date = [df dateFromString:string];
[timeArray addObject:time];
}
[df release]; // ALWAYS release unused objects
Then to just access hours, and not days, you should select the required components of each NSDate* instance:
// Get the Gregorian calendar
NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
// Get the date
NSDate* date = [timeArray objectAtIndex:my_index];
// Get the hours, minutes, seconds
NSDateComponents* hour = [cal components:NSHourCalendarUnit fromDate:date];
NSDateComponents* minute = [cal components:NSMinuteCalendarUnit fromDate:date];
NSDateComponents* second = [cal components:NSSecondCalendarUnit fromDate:date];
NSMutableArray *nsdateArray = [NSMutableArray arrayWithCapacity:[DateTimeArray count]];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd.MM.yyyy HH:mm:ss"];
for(NSString *currentString in DateTimeArray){
NSDate *date = [dateFormat dateFromString:currentString];
[nsdateArray addObject:date];
}
[dateFormat release];
NSLog(#"ArrayWithDate:%#",[nsdateArray description]);
Heres the complete set of code.. Hope it helps..
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"HH:mm:ss"];
int i=0;//start for{} loop
NSString *currDate = [formatter dateFromString:[DateTimeArray objectAtIndex:i]];
[formatter release];
Happy iCoding...
NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setDateFormat:#"HH:mm:ss"]
NSString *theTime = [timeFormat dateFromString:date];
or
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:(kCFCalendarUnitHour | kCFCalendarUnitMinute) fromDate:date];
NSInteger hour = [components hour];
NSInteger minute = [components minute];

NSDateFormater from NSMutableArray value

I store some dates in my NSMutableArray, I want to retrieve them and change the format.
For the moment I do this:
NSDate *myDate = [self.dates objectAtIndex:0];
NSLog(#"myDate:%#", myDate); // 2010-03-02
NSDateFormatter *formatDate = [[NSDateFormatter alloc] init];
[formatDate setDateFormat:#"MMMM d YYYY"];
NSString *newDate = [formatDate stringFromDate:myDate];
NSLog(#"newDate: %#", newDate); // NULL
the result for newDate is NULL, and I want it to be something like that: "March 3, 2010"
thanks,
That works for me, but I replaced the first line with:
NSDate *myDate = [NSDate date];
My output:
2010-03-02 19:29:08.045 app[11464:a0f] myDate:2010-03-02 19:29:08 -0800
2010-03-02 19:29:08.048 app[11464:a0f] newDate: March 2 2010
My best guess is that [self.dates objectAtIndex:0] isn't returning what you want it to.
Since your dates array contains strings, you have to convert the string to an NSDate and then you can re-convert the date to the format you want.
NSDateFormatter *strToDateFmt = [[NSDateFormatter alloc] init];
[strToDateFmt setDateFormat:#"yyyy-MM-dd"];
NSDate *myDate = [strToDateFmt dateFromString:[self.dates objectAtIndex:0]];
[strToDateFmt release];
NSLog(#"myDate:%#", myDate);
NSDateFormatter *formatDate = [[NSDateFormatter alloc] init];
[formatDate setDateFormat:#"MMMM d YYYY"];
NSString *newDate = [formatDate stringFromDate:myDate];
[formatDate release];
NSLog(#"newDate: %#", newDate);