Get name of days between two date in ios? - objective-c

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];

Related

we get string form web service that string how to convert NsString to NSDate in objective c?

i have tried like this
cell.msgid.text=[[tableArray objectAtIndex:indexPath.row]objectForKey:#"MobileMessageMasterId"];
NSString *datefromweb= [[tableArray objectAtIndex:indexPath.row] objectForKey:#"MessageDateTime"];
NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
[dateformatter setDateFormat:#"dd/MM/yyyy hh:mm:ss"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateformatter dateFromString:datefromweb];
[dateformatter setDateFormat:#"MMM yyyy"];
NSString *month1 = [dateformatter stringFromDate:dateFromString];
[dateformatter setDateFormat:#"hh:mm a"];
NSString *time= [dateformatter stringFromDate:dateFromString];
[dateformatter setDateFormat:#"dd"];
NSString *day1= [dateformatter stringFromDate:dateFromString];
we get string in datefromweb(NSString) variable but not get in
datefromstring(NSDate) variable and i am separate out date,time,month,year.
my response is
{ MessageDateTime = "19/01/2016 14:16:03";}
Try like this:
NSString *yourString = #""; // use your string here where you will get from JSON
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setTimeZone:[NSTimeZone localTimeZone]]; // Set timezone whatever you want
[formatter setDateFormat:#"dd/MM/yyyy HH:mm:ss"];
NSDate *convertedDate = [formatter dateFromString:yourString];
NSLog(#"%#",convertedDate);
//Get Date string from web
NSString *datefromweb= #"19/01/2016 14:16:03";
//Set Date Formatter
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"dd/MM/yyyy HH:mm:ss"];
//Convert string into Date
NSDate *selectedDate = [df dateFromString: datefromweb];
//For Setting new format to NSDate need to create new NSDateFormatter.
NSDateFormatter *df1 = [[NSDateFormatter alloc] init];
[df1 setDateFormat:#"dd"];
NSString* myDayString = [df1 stringFromDate:selectedDate];
[df1 setDateFormat:#"MMM"];
NSString* myMonthString = [df1 stringFromDate:selectedDate];
[df1 setDateFormat:#"MM"];
NSString* myMonthString1 = [df1 stringFromDate:selectedDate];
[df1 setDateFormat:#"yyyy"];
NSString* myYearString = [df1 stringFromDate:selectedDate];
[df1 setDateFormat:#"hh:mm a"];
NSString* myTimeString = [df1 stringFromDate:selectedDate];
Output :
Date 19
Month Name Jan - 01
Year 2016
Time 02:16 PM
Use NSDateComponents is better approach
NSDateFormatter *formatter = [NSDateFormatter new];
[formatter setDateFormat:#"dd/mm/yyyy HH:mm:ss"];
NSDate *date = [formatter dateFromString:#"19/01/2016 14:16:03"];
NSCalendar *gregorian = [NSCalendar currentCalendar];
NSDateComponents *comonents =
[gregorian components:(NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond
) fromDate:date];
NSInteger day = [comonents day];
NSInteger month = [comonents month];
NSInteger year = [comonents year];
NSInteger hour = [comonents hour];
NSInteger minutes = [comonents minute];
NSInteger seconds = [comonents second];

How to convert Nstring to NSDate?

i want to convert nsstring to nsdate
i have string have this value
08:00:00
i want to convert it so i wrote this code
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"HH:mm:ss"];
NSDate *startDate = [formatter dateFromString:_startTime];
NSLog(#"%#",startDate);
_startTime have this value = 08:00:00
but startDate after i display it , it had this value
2000-01-01 06:00:00 +0000
Update I wrote this code
NSString *dateString = _startTime;
NSDateFormatter *formatter= [[NSDateFormatter alloc] init];
// this is imporant - we set our input date format to match our input string
// if format doesn't match you'll get nil from your string, so be careful
[formatter setDateFormat:#"HH:mm:ss"];
NSDate *startDate = [[NSDate alloc] init];
// voila!
startDate = [formatter dateFromString:dateString];
NSLog(#"%#",startDate);
i want the output be 08:00:00
You can't have NSDate without a date !
if you want to access the time from an NSDate you'll need to use NSDateComponents (or NSTimeInterval).
To access the time from your NSDate using NSDateComponents :
NSString *startTime = #"08:00:00";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"HH:mm:ss"];
NSDate *startDate = [formatter dateFromString:startTime];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:(NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond) fromDate:startDate];
NSLog(#"%d:%d:%d", [components hour], [components minute], [components second]);
I don't get what's the purpose of changing the string _startTime, do you want to compare dates ?
Edit :
NSString *startTime = #"08:00:00";
NSString *endTime = #"23:00:00";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"HH:mm:ss"];
NSDate *startDate = [formatter dateFromString:startTime];
NSDate *endDate = [formatter dateFromString:endTime];
NSTimeInterval difference = [endDate timeIntervalSinceDate:startDate];
NSInteger temp = difference;
NSDate* newDate = [[NSDate alloc] init];
for (int i = 0; (i < difference && [newDate compare:startDate] == NSOrderedDescending); ++i)
{
newDate = [startDate dateByAddingTimeInterval:(temp - i*60*10)];
NSLog(#"%#",newDate);
}
run this and use NSDateComponents to log what you want !
Use this code i think this one help you..
NSString *dateStr = #"08:00:00";
NSDateFormatter *datFormatter = [[NSDateFormatter alloc] init];
[datFormatter setDateFormat:#"HH:mm:ss"];
NSDate* mydate = [datFormatter dateFromString:dateStr];
NSLog(#"date: %#", [datFormatter stringFromDate:mydate]);
i think this is what you really want...

Display list of month in that particular year in XCode 5

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);
}

add current day with one day after and one day before to date picker objective-C

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;
}

Not removing the Time Components from NSDate

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]];
}