I am sorry, as this question may have been asked before, but I could not find an answer that worked in my situation.
I am new to Objective-C, and I am not entirely knowledgable, so I apologize beforehand in the case that I seem like I am not amazing ;)
So, I have a webView, and everyday, the url changes. Ex: on April 30th the url is http://example.com/mylinkApr30
Using dates, I made a variable (sorry if my terminology is off :\ ), and the url ends in %d everything works just fine, except when it comes to month. The months are not in the typical MM form, they are in a shortened text, with the three first letters of the month name. Ex: Jan, Feb, Mar, Apr, etc.
I have the month integer working, and it writes as 1,2,3 etc.
How should I go about changing that to Jan, Feb, Mar?
Is there a different way I could go about this??
I can confirm the days are working, I have tested it with having 1 variable, and using Apr at the end of the link.
Here is my code, so it is easier for you to understand what I am asking.
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.title = #"Balmoral";
NSCalendar *calendar= [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSCalendarUnit unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |
NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit |
NSSecondCalendarUnit;
NSDate *date = [NSDate date];
NSDateComponents *dateComponents = [calendar components:unitFlags fromDate:date];
//NSInteger year = [dateComponents year];
NSInteger month = [dateComponents month];
NSInteger day = [dateComponents day];
//NSInteger hour = [dateComponents hour];
//NSInteger minute = [dateComponents minute];
//NSInteger second = [dateComponents second];
NSString *baseURLStr = #"http://wwww.WebsiteHere.com/Apr";
NSURL *url = [NSURL URLWithString:[baseURLStr stringByAppendingFormat:#"%d.ashx", day]];
[webView loadRequest:[NSURLRequest requestWithURL:url]];
[NSCalendar release];
}
Check out the NSDateFormatter class, something like:
int month = 3;
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MM"];
NSDate *date = [formatter dateFromString:[NSString stringWithFormat:#"%02i", month]];
[formatter setDateFormat:#"MMM"];
NSString *monthString = [formatter stringFromDate:date];
NSLog(#"MONTH STRING %#", monthString);
monthString should be "Mar"
A simple, and likely naive, approach would be to have an array with the text you want:
NSArray *months = [NSArray arrayWithObjects:#"Jan", #"Feb", nil];
This would allow you to get the string by:
NSString *monthString = [months objectAtIndex:month];
,Since you are not using the standard MM format you cannot use NSDateFormatter. You need a switch for all 12 months.
NSString * monthString = nil;
switch (month)
{
case 1:
monthString = #"Jan";
break;
case 2:
...
}
Then modify your own code:
NSString *baseURLStr = #"http://wwww.WebsiteHere.com/";
NSURL *url = [NSURL URLWithString:[baseURLStr stringByAppendingFormat:#"%#%d.ashx", monthString, day]];
EDIT:
Ryan is right. his solution is better.
Related
Is it possible to add the day name to the date picker?
For example to dials will show:
(sunday)3 | 11 | 2012
is it even possible?
Thanks!
I think you can not DayName(eg. Sunday) with Year-Month-Day until you not make it totally custom but you can get the day with date by following code may be It will help you.
Bind following two IBOutlet in Xib.
and bind method -(IBAction)GetDateWithDay with DatePicker with ValueChange attribut
in .h file
IBOutlet UIDatePicker* datePicker;
IBOutlet UILabel* lblDate;
in .m file
-(IBAction)GetDateWithDay
{
NSDate* dt = datePicker.date;
NSDateFormatter* df = [[[NSDateFormatter alloc]init]autorelease];
[df setDateFormat:#"yyyy-MM-dd"];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSInteger units = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit;
NSDateComponents *components = [calendar components:units fromDate:dt];
NSInteger year = [components year];
NSInteger day = [components day];
NSDateFormatter *weekDay = [[[NSDateFormatter alloc] init] autorelease];
[weekDay setDateFormat:#"EEEE"];
NSDateFormatter *calMonth = [[[NSDateFormatter alloc] init] autorelease];
[calMonth setDateFormat:#"MM"];
lblDate.text = [NSString stringWithFormat:#"%#, %i-%#-%i",[weekDay stringFromDate:dt], day, [calMonth stringFromDate:dt], year];
}
Read this document It will be helpful to you
Check out this code :
http://code4app.net/ios/FlatDatePicker/51cce1d76803fa4b0c000003
http://code4app.net/ios/Custom-DatePicker/51f667d36803fab73f000001
Hope this will solve your problem
You can use the option DD.
For Example.
$.datepicker.formatDate('DD', new Date(2007, 1 - 1, 26));
Hope this will help you. UI/Datepicker/formatDate
Here I'm trying to calculate the hours between two dates. When i run the application, it crashes. Could you please tell me the mistake in this code?
NSString *lastViewedString = #"2012-04-25 06:13:21 +0000";
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat: #"yyyy-MM-dd HH:mm:ss zzz"];
NSDate *lastViewed = [[dateFormatter dateFromString:lastViewedString] retain];
NSDate *now = [NSDate date];
NSLog(#"lastViewed: %#", lastViewed); //2012-04-25 06:13:21 +0000
NSLog(#"now: %#", now); //2012-04-25 07:00:30 +0000
NSTimeInterval distanceBetweenDates = [now timeIntervalSinceDate:lastViewed];
double secondsInAnHour = 3600;
NSInteger hoursBetweenDates = distanceBetweenDates / secondsInAnHour;
NSLog(#"hoursBetweenDates: %#", hoursBetweenDates);
Referring to this answer of a mostly similar question a better and Apple approved way would be using the NSCalendar methods like this:
- (NSInteger)hoursBetween:(NSDate *)firstDate and:(NSDate *)secondDate {
NSUInteger unitFlags = NSCalendarUnitHour;
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [calendar components:unitFlags fromDate:firstDate toDate:secondDate options:0];
return [components hour]+1;
}
If you target iOS 8 or later use NSCalendarIdentifierGregorian instead of the deprecated NSGregorianCalendar.
I think difference should be in int value...
NSLog(#"hoursBetweenDates: %d", hoursBetweenDates);
Hope, this will help you..
NSInteger can't be shown by using
NSLog(#"%#", hoursBetweenDates);
instead use:
NSLog(#"%d", hoursBetweenDates);
If unsure what to use look in the Apple Developer Docs:
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html#//apple_ref/doc/uid/TP40004265
i use this code to get informations from strings with this format "01-05-2011"
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:#"dd'-'MM'-'yyyy"];
// Your date represented as a NSDate
NSDate *dateDepart = [formatter dateFromString:daparatureFly.date];
NSDateComponents *comps = [[NSCalendar currentCalendar] components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit
fromDate:dateDepart];
Then when i take the month like this [comps day],[comps month] , i have 1 and 5 , i want to have 01, 05 . Any one can help me ? thanx
Those components are NSSintegers, they are not formatted. When you use it in string you can do as follows:
NSString *day = [NSString stringWithFormat:#"%02d", [comps day]];
Something akin to...
//UNTESTED
[NSString stringWithFormat:#"%02d", [comps day]];
I want some changes in the date comparison.
In my application I am comparing two dates and getting difference as number of Days, but if there is only one day difference the system shows me 0 as a difference of days.
NSDateFormatter *date_formater=[[NSDateFormatter alloc]init];
[date_formater setDateFormat:#"MMM dd,YYYY"];
NSString *now=[NSString stringWithFormat:#"%#",[date_formater stringFromDate:[NSDate date]]];
LblTodayDate.text = [NSString stringWithFormat:#"%#",[NSString stringWithFormat:#"%#",now]];
NSDate *dateofevent = [[NSUserDefaults standardUserDefaults] valueForKey:#"CeremonyDate_"];
NSDate *endDate =dateofevent;
NSDate *startDate = [NSDate date];
gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
unsigned int unitFlags = NSDayCalendarUnit;
NSDateComponents *components = [gregorian components:unitFlags fromDate:startDate toDate:endDate options:0];
int days = [components day];
I found some solutions that If we make the time as 00:00:00 for comparison then it will show me proper answer, I am right or wrong I don't know.
i think this is not correct make the time 00:00:00.
may be you get difference is less than 24 hour thats why it rounded off and you 0 day.
Alexander solution is right so use that solution like -
this works fine for me also.
NSDate *endDate=[dateFormat dateFromString:now];
NSTimeInterval interval = [CeremonyDate timeIntervalSinceDate:endDate];
int diff=interval/86400;//for converting seconds into days.
same problem of rounding a figure you get here but you can sort out that in an understable way.
I usually find out difference in seconds and calculate ceil(diffInSeconds / 86400).
Try this code for get two date and time different.
NSDateFormatter* dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"] autorelease]];
[dateFormatter setDateFormat:#"mm:ss"];
NSDate* firstDate = [dateFormatter dateFromString:#"04:45"];
NSDate* secondDate = [dateFormatter dateFromString:#"05:00"];
NSTimeInterval timeDifference = [secondDate timeIntervalSinceDate:firstDate];
NSLog(#"%f",timeDifference);
i hope this code usefull for you.
Here a prefect solution to find difference between two dates
- (NSString *)calculateDuration:(NSDate *)oldTime secondDate:(NSDate *)currentTime
{
NSString *timeSincePost;
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSCalendarUnit unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *dateComponents = [calendar components:unitFlags fromDate:oldTime toDate:currentTime options:0];
NSInteger year = [dateComponents year];
NSInteger month = [dateComponents month];
NSInteger day = [dateComponents day];
NSInteger hour = [dateComponents hour];
NSInteger minute = [dateComponents minute];
NSInteger second = [dateComponents second];
if (year) {
timeSincePost = [NSString stringWithFormat:#"%ld%#", (long)year,[[kAppDelegate languageBundle] localizedStringForKey:#"y" value:#"" table:nil]];
}
else if (month) {
timeSincePost = [NSString stringWithFormat:#"%ld%#", (long)month,[[kAppDelegate languageBundle] localizedStringForKey:#"M" value:#"" table:nil]];
}
if(day) {
timeSincePost = [NSString stringWithFormat:#"%ld%#", (long)day,[[kAppDelegate languageBundle] localizedStringForKey:#"d" value:#"" table:nil]];
}
else if(hour) {
timeSincePost = [NSString stringWithFormat: #"%ld%#", (long)hour,[[kAppDelegate languageBundle] localizedStringForKey:#"H" value:#"" table:nil]];
}
else if(minute) {
timeSincePost = [NSString stringWithFormat: #"%ld%#", (long)minute,[[kAppDelegate languageBundle] localizedStringForKey:#"m" value:#"" table:nil]];
}
else if(second)
timeSincePost = [NSString stringWithFormat: #"%ld%#", (long)second,[[kAppDelegate languageBundle] localizedStringForKey:#"s" value:#"" table:nil]];
return timeSincePost;
}
and call above function with two parameter as NSDate
NSString *duration = [self calculateDuration:postDate secondDate:[NSDate date]];
lblPostTime.text = duration;
note:: postDate is FirstDate & second date is current date..
In my application I'm using following codes to retrieve current date and day :-
NSDate *today1 = [NSDate date]; NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setDateFormat:#"dd/MM/yyyy :EEEE"]; NSString *dateString11 = [dateFormat stringFromDate:today1];
NSLog(#"date: %#", dateString11);
//[dateFormat release];
NSCalendar *gregorian11 = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components1 = [gregorian11 components:NSWeekdayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:today1];
[components1 setDay:([components1 day]-([components1 weekday]-1))];
NSDate *beginningOfWeek1 = [gregorian11 dateFromComponents:components1];
NSDateFormatter *dateFormat_first = [[NSDateFormatter alloc] init];
[dateFormat_first setDateFormat:#"dd/MM/yyyy :EEEE"];
NSString *dateString_first = [dateFormat_first stringFromDate:beginningOfWeek1];
NSLog(#"First_date: %#", dateString_first);
[components1 setDay:([components1 day]-([components1 weekday]-1) + 6)]; now =[gregorian dateFromComponents:components1]; [format setDateFormat:#"dd/MM/yyyy :EEEE"]; dateString = [format stringFromDate:now]; NSLog(#" week Last_date: %#", dateString);
but using above code I only got the current day and Date and first day of week but I need to get last day of week. But it gives the wrong output. Where am I wrong in my code and what modification is needed to get last day/date of week?
When you call setDay: you are sometimes setting it to a negative day. I don't know if setDay and/or dateFromComponents: will handle that.
To create a NSDate for a date/time that is in the past (or future) you can subtract (or add) the number of seconds that you want to go back (or forward), like this:
// convert to seconds
NSTimeInterval tmpSecs = [[NSDate date] timeIntervalSinceReferenceDate];
// Shift the date/time (in seconds) to a new date X days away:
tmpSecs += daysOffset * 86400; // 86400 seconds per day
// convert back to NSDate and return the result
return [NSDate dateWithTimeIntervalSinceReferenceDate:tmpSecs];