I need to remove the time components from NSDate for instance: I have the value of '2012-08-12 22:15:54 +00000' but I got the value '2012-08-12 03:00:00 +00000'
The Code I am Using is:
NSDate* getNDateOfWeek(int week, int day) {
NSDate *now = [NSDate date];
NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
calendar.timeZone = [NSTimeZone systemTimeZone];
unsigned unitFlags = NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit;
NSDateComponents *comp = [calendar components:unitFlags fromDate:now];
[comp setWeekday:day];
[comp setWeek: week];
[comp setHour:0];
NSDate *resultDate = [calendar dateFromComponents:comp];
return resultDate;
}
Any help?
You might be trying to achieve a different result but if you are simply looking for 'todays' date then what about something like:
// Output -> 'Mon, Jan 16, 2012'
- (NSString *)todaysDate
{
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"EEE MMM d Y"];
return [dateFormat stringFromDate:[[NSDate alloc] init]];
}
Related
I create a date picker programmatically for ios 7 and set maximum and minimum date.But it return wrong year for some particular december days.
[fromDatePicker addTarget:self action:#selector(fromDatePickerAction:) forControlEvents:UIControlEventValueChanged];
NSDate *date = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
NSDateComponents * components = [[NSDateComponents alloc] init];
NSDateComponents *monthComponents = [gregorian components:NSMonthCalendarUnit fromDate:date];
int currentMonth = [monthComponents month];
NSDateComponents *dayComponents = [gregorian components:NSDayCalendarUnit fromDate:date];
int dayss=[dayComponents day];
[components setYear:0];
NSDate * minDate = [gregorian dateByAddingComponents: components toDate: date options: 0];
[components setYear:0];
[components setMonth:12-currentMonth];
[components setDay:31-dayss];
NSDate * maxDate = [gregorian dateByAddingComponents: components toDate: date options: 0];
fromDatePicker.minimumDate=minDate;
fromDatePicker.maximumDate=maxDate;
[self.view addSubview:fromDatePicker];
- (void) fromDatePickerAction:(id)sender{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"YYYY-MM-dd hh:mm aa"];
[self.delegate didTouchAddNotePicker:[dateFormatter stringFromDate:[fromDatePicker date]]];
NSLog(#"%#",[dateFormatter stringFromDate:[fromDatePicker date]]);
//_lblDate.text=[dateFormatter stringFromDate:[fromDatePicker date]];
}
Here I choose this date shown in figure 1.But it returns the date in figure 2
Please help to solve this problem
This problem is solved by changing [dateFormatter setDateFormat:#"YYYY-MM-dd hh:mm aa"] to [dateFormatter setDateFormat:#"yyyy-MM-dd hh:mm aa"] in date picker selector method
Please click this link to know about the difference between YYYY and yyyy
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"yyyy";
NSDate *date = [dateFormatter dateFromString:#"2011"];
for(int month=0;month<=12;month++)
{
dateFormatter.dateFormat=#"MMMM";
NSString * monthString = [[dateFormatter stringFromDate:date] capitalizedString];
NSLog(#"month: %#", monthString);
}
I need to display all the month in a particular year.Example if i give the year as 2011 i want the entire 12 month in that year i used tis above code.
But the answer,i am getting one month as january that printed in 12 times ,but i need to get whole 12 months in the particular year.
Try this following code.
//NSInteger startingMonth = 1;
NSInteger startingYear = 2014;
// we'll need this in several places
NSCalendar *cal = [NSCalendar currentCalendar];
// build the first date (in the starting month and year)
NSDateComponents *comps = [[NSDateComponents alloc] init];
//[comps setMonth:startingMonth];
[comps setYear:startingYear];
// [comps setDay:1];
NSDate *date = [cal dateFromComponents:comps];
// this is our output format
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MMMM YYYY"];
NSDateFormatter *format1 = [[NSDateFormatter alloc] init];
[format1 setDateFormat:#"MM yy"];
// we need NSDateComponents for the difference, i.e. at each step we
// want to go one month further
NSDateComponents *comps2 = [[NSDateComponents alloc] init];
[comps2 setMonth:1];
for (int i= 0; i < 12; i++) {
NSLog(#"month list %#", [format1 stringFromDate:date]);
NSString *str=[NSString stringWithFormat:#"%#",[formatter stringFromDate:date]];
[mothlist_ary addObject:str];
NSLog(#"GET MONTH %#",mothlist_ary);
// add 1 month to date
date = [cal dateByAddingComponents:comps2 toDate:date options:0];
}
Try This...
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"yyyy";
NSDate *date = [dateFormatter dateFromString:#"2011"];
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *components = [cal components:( NSMonthCalendarUnit) fromDate:date];
dateFormatter.dateFormat=#"MMMM";
for(int month=1;month<=12;month++)
{
[components setMonth:(month)];
NSDate *lastMonth = [cal dateFromComponents:components];
NSString * monthString = [[dateFormatter stringFromDate:lastMonth] capitalizedString];
NSLog(#"month: %#", monthString);
}
I just found a but in one of my apps; the problem is that I am calculating the days in a menu using initWithTimeIntervalSinceNow and adding the caculation (day*X) (X meaning the day in question).
However, the night between 27th and 28th of oct the CEST timezone will become CET thus meaning that for one day the time should be calculated by (day*X)+3600. However, I do not want to use if cases and believe there should be a better way of dealing with this.
How can I calculate future days with keeping summer/winter time in mind?
My code:
int day = (60*60*24);
NSDate *today = [NSDate date];
NSDate *days1 = [[NSDate alloc] initWithTimeIntervalSinceNow:day];
NSDate *days2 = [[NSDate alloc] initWithTimeIntervalSinceNow:(day*2)];
NSDate *days3 = [[NSDate alloc] initWithTimeIntervalSinceNow:(day*3)];
NSDate *days4 = [[NSDate alloc] initWithTimeIntervalSinceNow:(day*4)];
NSDate *days5 = [[NSDate alloc] initWithTimeIntervalSinceNow:(day*5)];
NSDate *days6 = [[NSDate alloc] initWithTimeIntervalSinceNow:(day*6)];
NSDate *days7 = [[NSDate alloc] initWithTimeIntervalSinceNow:(day*7)];
Try this:
- (NSDate *)dateByAddingXDaysFromToday:(int)days
{
NSDateComponents *comps = [[NSDateComponents alloc] init];
comps.day = days;
return [[NSCalendar currentCalendar] dateByAddingComponents:comps toDate:[NSDate date] options:0];
}
Then you can use:
NSDate *days1 = [self dateByAddingXDaysFromToday:1];
//etc...
It's not clear what you are trying to achieve with calculating the date objects. I'm assuming you want to create objects that refer the next seven days, same time as now.
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setDay:1];
NSDate *now = [NSDate date];
NSDate *days1 = [calendar dateByAddingComponents:components toDate:now options:0];
NSDate *days2 = [calendar dateByAddingComponents:components toDate:days1 options:0];
NSDate *days3 = [calendar dateByAddingComponents:components toDate:days2 options:0];
....
I have a datepicker and I want to have just one row with 3 options: current day, one day after and one day before .
Would you help me to do that?
My question is, how can I add 'current day' with one day after and one day before ?
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setDay:days];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *end = [gregorian dateByAddingComponents:components toDate:[NSDate date] options:0];
Where days is an integer representing the days you want to add/subtract from the current day.
This is how i did-
-(void)getDate{
NSString *str_CurDate =[self getCurrentDate];
NSString *str_NextDay =[self getFormattedDate:[self dateByAddingDays:1]];
NSString *str_PrevDay =[self getFormattedDate:[self dateByAddingDays:-1]];
}
Now implement these method
-(NSString*)getCurrentDate{
NSDate *currDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"d/M/yyyy"];
NSString *dateString = [dateFormatter stringFromDate:currDate];
return dateString;
}
- (NSDate *) dateByAddingDays:(int)days {
NSDate *retVal;
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setDay:days];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
retVal= [gregorian dateByAddingComponents:components toDate:[NSDate date] options:0];
return retVal;
}
-(NSString*)getFormattedDate:(NSDate*)myDate{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"d/M/yyyy"];
NSString *dateString = [dateFormatter stringFromDate:myDate];
return dateString;
}
Encapsulate in a category on NSDate
- (NSDate *) dateByAddingDays:(int)days {
NSDate *retVal;
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setDay:days];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
retVal = [gregorian dateByAddingComponents:components toDate:self options:0];
return retVal;
}
How to get name of days between two dates in ios?
Example:
Input:
Start Date: 3-11-2012
End date: 5-11-2012
Output:
3-11-2012 Wednesday
4-11-2012 Thursday
5-11-2012 Friday
Thanks..
try this,
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd"];
NSDate *startDate = [df dateFromString:#"2012-07-20"]; // your start date
NSDate *endDate = [NSDate date]; // your end date
NSDateComponents *dayDifference = [[NSDateComponents alloc] init];
NSMutableArray *dates = [[[NSMutableArray alloc] init] autorelease];
NSUInteger dayOffset = 1;
NSDate *nextDate = startDate;
do {
[dates addObject:nextDate];
[dayDifference setDay:dayOffset++];
NSDate *d = [[NSCalendar currentCalendar] dateByAddingComponents:dayDifference toDate:startDate options:0];
nextDate = d;
} while([nextDate compare:endDate] == NSOrderedAscending);
[df setDateStyle:NSDateFormatterFullStyle];
for (NSDate *date in dates) {
NSLog(#"%#", [df stringFromDate:date]);
}
[df release];
Output is :
Friday, July 20, 2012
Saturday, July 21, 2012
Sunday, July 22, 2012
Monday, July 23, 2012
Tuesday, July 24, 2012
Check this solution:
NSDateComponents *components;
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd-MM-yyyy"];
NSDate *startDate = [dateFormat dateFromString:#"3-11-2012"];
NSDate *endDate = [dateFormat dateFromString:#"5-11-2012"];
[dateFormat setDateFormat:#"dd-MM-yyyy, EEEE"];
components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit fromDate:startDate toDate:endDate options:0];
int days = [components day];
for (int x = 0; x <= days; x++) {
NSLog(#"%#", [dateFormat stringFromDate:startDate]);
startDate = [startDate dateByAddingTimeInterval:(60 * 60 * 24)];
}
The output is:
03-11-2012, Saturday
04-11-2012, Sunday
05-11-2012, Monday
Here is one way to do it:
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
[dateComponents setYear:2012];
[dateComponents setMonth:11];
[dateComponents setDay:03];
NSDate *date1 = [gregorianCalendar dateFromComponents:dateComponents];
[dateComponents setYear:2012];
[dateComponents setMonth:11];
[dateComponents setDay:05];
NSDate *date2 = [gregorianCalendar dateFromComponents:dateComponents];
NSDateFormatter *outputDateFormatter = [[NSDateFormatter alloc] init];
[outputDateFormatter setDateFormat:#"dd-MM-yyyy EEEE"];
NSDate *dayDate;
dayDate = date1;
NSLog(#"%#", [outputDateFormatter stringFromDate:dayDate]);
do {
dayDate = [dayDate dateByAddingTimeInterval:86400];
NSLog(#"%#", [outputDateFormatter stringFromDate:dayDate]);
} while ([dayDate compare:date2]==NSOrderedAscending);
using NSCalendar gives you (and your user) greater flexibility for displaying and manipulating dates.
This is what you need:
NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
[inputFormatter setDateFormat:#"mm-dd-yyyy"];
NSDate *inputDate = [inputFormatter dateFromString:#"3-11-2012"];
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:#"mm-dd-yyyy EEEE"];
NSString *output = [outputFormatter stringFromDate:inputDate];
NSLog(#"%#",output);
[inputFormatter release];
[outputFormatter release];