I have a string 14-Sep-2011 In need to convert this 14th Sept. This date may be any date string. Is there any date formatter which allows me to convert date in my format. As if date is 1-Sept-2011 then I need 1st Sept, 2-Sept-2011 should say 2nd Sept.
Can anyone please suggest the solution.
Thanks
- (NSString *)ordinalSuffixFromInt:(int)number {
NSArray *cSfx = [NSArray arrayWithObjects:#"th", #"st", #"nd", #"rd", #"th", #"th", #"th", #"th", #"th", #"th", nil];
NSString *suffix = #"th";
number = abs(number % 100);
if ((number < 10) || (number > 19)) {
suffix = [cSfx objectAtIndex:number % 10];
}
return suffix;
}
Test:
- (void)test {
for (int day=1; day<=31; day++) {
NSLog(#"ordinal: %d%#", day, [self ordinalSuffixFromInt:day]);
}
}
You try the following code because I run successfully.
NSString *dateStr = #"14-Sep-2011";
NSDateFormatter *dtF = [[NSDateFormatter alloc] init];
[dtF setDateFormat:#"dd-MMM-yyyy"];
NSDate *d = [dtF dateFromString:dateStr];
NSDateFormatter *monthDayFormatter = [[[NSDateFormatter alloc] init] autorelease];
[monthDayFormatter setFormatterBehavior:NSDateFormatterBehaviorDefault];
[monthDayFormatter setDateFormat:#"d MMM"];
int date_day = [[monthDayFormatter stringFromDate:d] intValue];
NSString *suffix_string = #"|st|nd|rd|th|th|th|th|th|th|th|th|th|th|th|th|th|th|th|th|th|st|nd|rd|th|th|th|th|th|th|th|st";
NSArray *suffixes = [suffix_string componentsSeparatedByString: #"|"];
NSString *suffix = [suffixes objectAtIndex:date_day];
NSString *format = [NSString stringWithFormat:#"%d",date_day];
NSString *dateStrfff = [format stringByAppendingString:suffix];
NSLog(#"%#", dateStrfff);
[monthDayFormatter setDateFormat:#"MMM"];
NSString *ss = [monthDayFormatter stringFromDate:d];
NSLog(#"%#",ss);
NSString *final = [dateStrfff stringByAppendingString:ss];
NSLog(#"final string:---> %#",final);
This is a perfect solution that you want.
This will work for any number :
-(NSString *) ordinalSuffix: (NSInteger) day {
NSString *ordinalSuffix;
if(day%10 == 1 && day%100 != 11) ordinalSuffix = #"st";
else if(day%10 == 2 && day%100 != 12) ordinalSuffix = #"nd";
else if(day%10 == 3 && day%100 != 13) ordinalSuffix = #"rd";
else ordinalSuffix = #"th";
return ordinalSuffix;
}
Test :
-(void) test {
for(NSInteger i = 1; i < 200; i++)
NSLog(#"%d%#", i, [self ordinalSuffix:i]);
}
use it
NSString *string =#"14-Sep-2011";
NSArray *arr = [string componentsSeparatedByString:#"-"];
NSString *str1=[arr objectAtIndex:0];
str1=[str1 stringByAppendingString:#"th"];
NSString *final=[str1 stringByAppendingFormat:#" %#",[arr objectAtIndex:1]];
Related
Need to get the last modified time for IOS application inside the document folder with UTF time format?
Try following code, that will help
+ (NSString*) fnGetModifiedTime: (NSString*)inFolderPath {
NSFileManager* fm = [NSFileManager defaultManager];
NSDictionary* attrs = [fm attributesOfItemAtPath:inFolderPath error:nil];
int theDirModified = 0;
if (attrs != nil) {
NSDate *date = (NSDate*)[attrs objectForKey: NSFileModificationDate];
NSDate* theGlobalDate = [self toGlobalTime:date];
theDirModified = [theGlobalDate timeIntervalSince1970];
}
return [NSString stringWithFormat:#"%i",theDirModified];
}
+(NSDate *) toGlobalTime:(NSDate*) inDate
{
NSTimeZone *tz = [NSTimeZone defaultTimeZone];
NSInteger seconds = -[tz secondsFromGMTForDate: inDate];
return [NSDate dateWithTimeInterval: seconds sinceDate: inDate];
}
i have this code:
NSString * firstdDigitTest = [numberString substringToIndex:1];
if (! [firstdDigitTest isEqualToString:#"0"])
{
numberString = [numberString substringFromIndex:1];
self.firstImageName = [namefile stringByAppendingString:firstdDigitTest];
}
self.number = [numberString integerValue];
i check if the first number is not 0 and if it's not 0 i need to insert it to the firstImageName. But when i do it (using the substringFromIndex) and i try to use integerValue it's dosent work!
without the substringFromIndex it's works! :(
NSString *str0 = #"XXX10098";
NSString *str1 = [str0 substringWithRange:NSMakeRange(4, str0.length-4)];
NSLog(#"%#", str1);
NSLog(#"%d", [str1 intValue]);
2012-10-28 10:52:07.309 iFoto[5652:907] 0098
2012-10-28 10:52:07.312 iFoto[5652:907] 98
check [numberString intValue];
I'm simply trying to save this array to the user defaults and it will crash at random. Sometimes it works, sometimes it gives me the EXC_BAD_ACCESS. Am I not releasing something properly?
- (void)setTextValue:(NSString *)valueText indexToSet:(NSUInteger)index
{
[self.pageData replaceObjectAtIndex:index withObject:valueText];
[[NSUserDefaults standardUserDefaults] setObject:self.pageData forKey:#"mynotes"];
}
Here is the method that i've determined is causing the errors. It was a method already created by Xcode that I added my own custom code to.
- (nbookDataViewController *)viewControllerAtIndex:(NSUInteger)index storyboard:(UIStoryboard *)storyboard
{
nbookDataViewController *dataViewController = [storyboard instantiateViewControllerWithIdentifier:#"nbookDataViewController"];
if (self.pageData.count > 0 && index < self.pageData.count)
{
NSString *val = (NSString *)[self.pageData objectAtIndex:index];
dataViewController.dataObject = val;
}
else
{
NSDate *date = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MMMM d, YYYY"];
NSString *dateString = [dateFormat stringFromDate:date];
//[dateFormat release];
[self.pageData addObject:dateString];
dataViewController.dataObject = (NSString *)[self.pageData objectAtIndex:index];
}
dataViewController.myModel = (nbookModelController *)self;
dataViewController.dIndex = index;
//[self.mySaveData setObject:self.pageData forKey:#"mynotes"];
return dataViewController;
}
This tip will allow you code to break on the exception and let you check directly why this is happening:
https://stackoverflow.com/a/616526/46970
I want use iPhone calendar add Event.
I try ti.com.calendar module from github but in this module only save startTime EndTime, Title and details.
but, not use allDay repeat or not reminder.
How i use this. in calendar?
I also user notification for reminder. but, after delete event. the notification is not delete.
any suggestion is appreciated
bellow is peace of code. date for alarm is using to match work hours (users don't like to wakeup to do job :)
eventStore = [[EKEventStore alloc] init];
EKEvent *newEvent = [EKEvent eventWithEventStore:eventStore];
newEvent.calendar = eventStore.defaultCalendarForNewEvents;
NSString *titleForEvent = [NSString stringWithFormat:#"In country:%# will be:\n%# event",[mo valueForKey:#"name"],[mo valueForKey:#"necessaryData"]];
newEvent.title = titleForEvent;
newEvent.allDay = YES;
NSDate *date = [mo valueForKey:#"date"];
NSDate *dateAlarm = [mo valueForKey:#"dateAlarm"];
EKAlarm *alarm = [EKAlarm alarmWithRelativeOffset:[dateAlarm timeIntervalSinceDate:date]];
if (dateAlarm < [NSDate date]){
dateAlarm = [NSDate dateWithTimeIntervalSinceNow:+18000];
NSDateFormatter *dateForm = [[NSDateFormatter alloc]init];
[dateForm setDateFormat:#"%HH"];
NSString *hourOfAlarm = [dateForm stringFromDate:dateAlarm];
[dateForm release];
NSNumberFormatter *numberForm = [[NSNumberFormatter alloc] init];
NSNumber *hour = [numberForm numberFromString:hourOfAlarm];
[numberForm release];
int difference = 0;
if ([hour intValue] < 9) difference = (9 - [hour intValue]) *3600;
if ([hour intValue] > 17) difference = (17 - [hour intValue]) *3600;
if (difference != 0) {
NSTimeInterval interval = 18000 + difference;
dateAlarm = [NSDate dateWithTimeIntervalSinceNow:interval];
}
alarm = [EKAlarm alarmWithRelativeOffset:[dateAlarm timeIntervalSinceDate:date]];
}
newEvent.startDate = date;
newEvent.endDate = date;
//EKAlarm *alarm = [EKAlarm alarmWithRelativeOffset:[dateAlarm timeIntervalSinceDate:date]];
newEvent.alarms = [NSArray arrayWithObject:alarm];
NSError *error;
BOOL saved = [eventStore saveEvent:newEvent span:EKSpanThisEvent error:&error];
if (!saved && error) {
NSLog(#"%#",[error localizedDescription]);
} else [mo setValue:newEvent.eventIdentifier forKey:#"eventIdentifier"];
how can I display 5 raise to 1/3 in iphone i.e I want 1/3 written above 5 can anyone help please
I Found this solution, hope so it would be helpful for you.
x to the power of y in a UILabel could be easy. Just replace your indices with unicode superscript characters... I use the following method to turn an integer into a string with superscript characters.
+(NSString *)convertIntToSuperscript:(int)i
{
NSArray *array = [[NSArray alloc] initWithObjects:#"⁰", #"¹", #"²", #"³", #"⁴", #"⁵", #"⁶", #"⁷", #"⁸", #"⁹", nil];
if (i >= 0 && i <= 9) {
NSString *myString = [NSString stringWithFormat:#"%#", [array objectAtIndex:i]];
[array release];
return myString;
}
else {
NSString *base = [NSString stringWithFormat:#"%i", i];
NSMutableString *newString = [[NSMutableString alloc] init];
for (int b = 0; b<[base length]; b++) {
int temp = [[base substringWithRange:NSMakeRange(b, 1)] intValue];
[newString appendString:[array objectAtIndex:temp]];
}
[array release];
NSString *returnString = [NSString stringWithString:newString];
[newString release];
return returnString;
}
}
Try this NSString *cmsquare=#"cm\u00B2";
It will display cm².
Yes you can do that but you need custom UILabel, either Make it by yourself or Get it Open Source..