i want a day in digits from my Current date.for an example
if entry_date = August 22, 2011
due_on = "Due on 22nd"
i know the logic of getting suffix,but how to get day value from my current date.
You can get day from current date as follows:
NSDate * mCurrentDate = [NSDate date];
NSCalendar * calendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents * weekdayComponents = [calendar components:NSDayCalendarUnit fromDate: mCurrentDate];
_day = [weekdayComponents day];
This will give you the day component of your current date.
(According to your example) try this ....
NSString *entry_date = #"August 22, 2011";
NSString *digits = [entry_date stringByTrimmingCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]];
int intDate = [digits intValue]
NSLog(#"Attempts: %d", intDate);
Hope its helps you... :D
Related
I have start date and a duration (period). For example startDate = '2014-02-12' period = 2. I desired dates 2014-02-12, 2014-02-14, 2014-02-16, .... I need to determine the current date is flagged during.
To check if the difference between the start date and the current date is an even
number of days, use NSDateComponents:
NSDate *startDate = ...;
NSDate *currentDate = [NSDate date];
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *diff = [cal components:NSDayCalendarUnit fromDate:startDate
toDate:currentDate options:0];
NSInteger days = diff.day;
if (days % 2 == 0) {
// even number of days between start date and current date
}
You can get the desired dates using
- (id)dateByAddingTimeInterval:(NSTimeInterval)seconds
with seconds = period*3600*24
Calc the days from startDate to givenData, check the result whether it can be divisible by the period.
+ (NSInteger)daysWithinEraFromDate:(NSDate *)startDate toDate:(NSDate *)endDate {
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
[gregorian setTimeZone:[NSTimeZone localTimeZone]];
// for timezone issue
NSDate *newDate1 = [startDate dateByAddingTimeInterval:[[NSTimeZone localTimeZone] secondsFromGMT]];
NSDate *newDate2 = [endDate dateByAddingTimeInterval:[[NSTimeZone localTimeZone] secondsFromGMT]];
NSInteger startDay = [gregorian ordinalityOfUnit:NSDayCalendarUnit
inUnit: NSEraCalendarUnit forDate:newDate1];
NSInteger endDay = [gregorian ordinalityOfUnit:NSDayCalendarUnit
inUnit: NSEraCalendarUnit forDate:newDate2];
return endDay - startDay;
}
I'm doing some weather app, in that i want to show the forecast for 4days i.e., current day to next 3days. I'm getting current day and the code is
NSCalendar* cal = [NSCalendar currentCalendar];
NSDateComponents* comp = [cal components:NSWeekdayCalendarUnit fromDate:[NSDate date]];
// 1 = Sunday, 2 = Monday, etc.
NSInteger day0 = comp.weekday;
NSLog(#"My DAY is %i", day0);
NSString *str;
NSMutableString *myString = [NSMutableString string];
str = [NSString stringWithFormat:#"%d",day0]; //%d or %i both is ok.
[myString appendString:str];
NSLog(#"My DAY is %#", myString);
And the output is 6 ie., Friday .... thats cool
How can i get the next day without incrementing the current day... I tried to increment the current day, but the problem is, if i get the current day as 7, it is incrementing to 8 no such thing in weedays right.. I'm newbie to xcode.. Help me out guys....
try the following:
NSDateComponents *dayComponent = [NSDateComponents new];
dayComponent.day = 1;
today = [calendar dateByAddingComponents:dayComponent toDate:today options:0];
"How can i get the next day without incrementing the current day..."
If you want a NSDate object representing each day, you can use NSDateComponents to add up:
NSDate *today = [NSDate date];
NSDateComponents *offset = [[NSDateComponents alloc] init];
offset.day = 1; // create a one day offset
NSDate *tomorrow = [[NSCalendar currentCalendar] dateByAddingComponents:offset toDate:today options:0];
Then get your weekday:
[[NSCalendar currentCalendar] components: NSWeekdayCalendarUnit fromDate:tomorrow];
"I tried to increment the current day, but the problem is, if i get the current day as 7, it is incrementing to 8 no such thing in weedays right.."
If it is enough to just increase the weekday of today, use a simple modulo (that's just basic in any programming language):
int weekdayOfTomorrow = (++weekdayOfToday % 7) + 1;
I have been on SO for awhile now trying to get this problem solved but have not had any luck.
In a nutshell I want to take a string like this: "2011-11-21 11:20:00" and calculate the "Time Until".
In a format like "1 day 36 mins" which would be a string I could display in a label.
I cant wrap my head around this. Anyone have some sample code from doing this before? Any help would be greatly appreciated.
#Maudicus is on the right track but has some flaws in his answer.
The date format you'd need to use is #"yyyy-MM-dd HH:mm:ss"
Once you have the date, you should use -[NSCalendar components:fromDate:toDate:options:] to figure out the differences between one date and another. For example:
NSDate *date = ...; // the date converted using the date formatter
NSDate *target = [NSDate date]; // now
NSCalendar *calendar = [NSCalendar currentCalendar];
NSUInteger components = NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *difference = [calendar components:components fromDate:date toDate:target options:0];
NSLog(#"difference: %d days, %d hours, %d minutes, %d seconds", [difference days], [difference hours], [difference minutes], [difference seconds]);
This code should help you out.
NSDate * date;
//Assume dateString is populated and of format NSString * dateString =#"2011-11-21 11:20:00";
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
//EDITED date formatter to correct behavior
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
if (dateString != nil) {
date = [dateFormatter dateFromString:dateString];
}
[dateFormatter release];
NSTimeInterval difference = [date timeIntervalSinceDate:[NSDate date]];
int days = difference/(3600*24);
difference -= (float) (days*3600*24);
int hours = difference/(3600);
difference -= (float) (hours*3600);
int minutes = difference/(60);
NSString * timeRemaining = [NSString stringWithFormat:#"%dd %dh %dm", days, hours, minutes];
duration.text = [NSString stringWithFormat:#"%d:%02d", (int)audioPlayer.duration / 3600, (int)audioPlayer.duration % 60, nil];
I have one query regarding NSDate. I have a date i.e. "2011-10-04 07:36:38 +0000", and I want to check if this date is yesterday, or today or a future date.
How would I go about this?
Try this:
Note: Change the date format as per your need.
NSDateFormatter* df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"MM/dd/yyyy"];
NSDate* enteredDate = [df dateFromString:#"10/04/2011"];
NSDate * today = [NSDate date];
NSComparisonResult result = [today compare:enteredDate];
switch (result)
{
case NSOrderedAscending:
NSLog(#"Future Date");
break;
case NSOrderedDescending:
NSLog(#"Earlier Date");
break;
case NSOrderedSame:
NSLog(#"Today/Null Date Passed"); //Not sure why This is case when null/wrong date is passed
break;
}
See Apple's documentation on date calculations:
NSDate *startDate = ...;
NSDate *endDate = ...;
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
NSUInteger unitFlags = NSMonthCalendarUnit | NSDayCalendarUnit;
NSDateComponents *components = [gregorian components:unitFlags
fromDate:startDate
toDate:endDate options:0];
NSInteger months = [components month];
NSInteger days = [components day];
If days is between +1 and -1 then your date is a candidate for being "today". Obviously you'll need to think about how you handle hours. Presumably the easiest thing would be to set all dates to be 00:00.00 hours on the day in question (truncate the date using an approach like this), and then use those values for the calculation. That way you'd get 0 for today, -1 for yesterday, +1 for tomorrow, and any other value would likewise tell you how far things were in the future or the past.
Use any of the folowing according to ur need,
– earlierDate:
– laterDate:
– compare:
Refer this http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDate_Class/Reference/Reference.html
-(NSString*)timeAgoFor:(NSString*)tipping_date
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSDate *date = [dateFormatter dateFromString:tipping_date];
NSString *key = #"";
NSTimeInterval ti = [date timeIntervalSinceDate:[NSDate date]];
key = (ti > 0) ? #"Left" : #"Ago";
ti = ABS(ti);
NSDate * today = [NSDate date];
NSComparisonResult result = [today compare:date];
if (result == NSOrderedSame) {
return[NSString stringWithFormat:#"Today"];
}
else if (ti < 86400 * 2) {
return[NSString stringWithFormat:#"1 Day %#",key];
}else if (ti < 86400 * 7) {
int diff = round(ti / 60 / 60 / 24);
return[NSString stringWithFormat:#"%d Days %#", diff,key];
}else {
int diff = round(ti / (86400 * 7));
return[NSString stringWithFormat:#"%d Wks %#", diff,key];
}
}
how can I calculate the calendar week? A year has 52/53 weeks and there are two rules:
-USA
-DIN 1355 / ISO 8601
I'd like to work with DIN 1355 / ISO 8601. How can I manage that?
Edit:
NSDate *today = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"ww"];
NSString *weeknumber = [dateFormat stringFromDate: today];
NSLog(#"week: %#", weeknumber);
Taken from http://iosdevelopertips.com/cocoa/date-formatter-examples.html
Where do I find the allowed date formats?
Use an NSCalendar and NSDateComponents.
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *components = [cal components:NSWeekCalendarUnit fromDate:date];
NSInteger week = [components week];
Or use:
CFAbsoluteTime currentTime = CFAbsoluteTimeGetCurrent();
CFTimeZoneRef currentTimeZone = CFTimeZoneCopyDefault();
SInt32 weekNumber = CFAbsoluteTimeGetWeekOfYear(currentTime, currentTimeZone);
The numbering follows the ISO 8601 definition of week.
NSDate *today = [NSDate date];
NSCalendar *ISO8601 = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierISO8601];
ISO8601.firstWeekday = 2; // Sunday = 1, Saturday = 7
ISO8601.minimumDaysInFirstWeek = 4;
NSDateComponents *components = [ISO8601 components:NSCalendarUnitWeekOfYear fromDate:today];
NSUInteger weekOfYear = [components weekOfYear];
NSDate *mondaysDate = nil;
[ISO8601 rangeOfUnit:NSCalendarUnitYearForWeekOfYear startDate:&mondaysDate interval:NULL forDate:today];
NSLog(#"The current Weeknumber of Year %ld ", weekOfYear);
You should user NSDateFormatter like so:
NSDateFormatter *fm = [[NSDateFormatter alloc] initWithDateFormat:#"ww" allowNaturalLanguage:NO];
NSString *week = [fm stringFromDate: date];
There is many kinds of Canlendar.
Week number according to the ISO-8601 standard, weeks starting on Monday. The first week of the year is the week that contains that year's first Thursday (='First 4-day week'). The highest week number in a year is either 52 or 53. This year has 52 weeks.This is not the only week numbering system in the world, other systems use weeks starting on Sunday (US) or Saturday (Islamic).
More details: http://www.epochconverter.com/weeknumbers
And Apple does support that, so you could find correct way referred from https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSCalendar_Class/
For example, with ISO-8601 standard:
NSCalendar *ISO8601 = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierISO8601];
Hope this could help.