I build label with the date for today (hebrew date)
its work but is number and not character
for exapmle:28/4 to 28 Tavat
code:
NSLocale *hebrew = [[NSLocale alloc] initWithLocaleIdentifier:#"he_IL"]; // Hebrew, Israel
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSHebrewCalendar];
NSDate *today = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
dateFormat.locale = hebrew;
dateFormat.calendar = calendar;
[dateFormat setDateStyle:NSDateFormatterShortStyle];
NSString *dateString = [dateFormat stringFromDate:today];
[_label setText:dateString];
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterLongStyle];
[formatter setTimeStyle:NSDateFormatterNoStyle];
NSLog(#"hebrew: %#", [formatter stringFromDate:[NSDate date]]);
[formatter release];
You need to change the style to medium or higher:
[dateFormat setDateStyle:NSDateFormatterMediumStyle];
If you use the medium style then the month will be abbreviated, switch to the long style (NSDateFormatterLongStyle) for the full month name.
Related
In some scenario, I am getting currentDateString as nil.My app is in live so i can't find out exact scenario.
NSDate *currentDate = [NSDate date];
NSCalendar *myCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm"];
NSString *currentDateString = [dateFormatter stringFromDate:currentDate];
I tried this code. I got correct results. Please set DateFormatter Locale
NSDate *currentDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[NSLocale localeWithLocaleIdentifier:#"en_US_POSIX"]];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm"];
NSString *currentDateString = [dateFormatter stringFromDate:currentDate];
I am using the following code to get the NSString from date
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = kCFDateFormatterLongStyle;
df.timeStyle = kCFDateFormatterNoStyle;
df.doesRelativeDateFormatting = YES;
[df setLocale:[NSLocale currentLocale]];
[df setTimeZone:[NSTimeZone systemTimeZone]];
NSString *dateString=[df stringFromDate:myDate];
But it displaying like 'Today','Yesterday' even after changing Locale.I want to display the localized language of that also ?
I used your code and created this:
NSDate *yesterday;
NSTimeInterval interval;
NSCalendar *cal = [NSCalendar currentCalendar];
[cal rangeOfUnit:NSDayCalendarUnit
startDate:&yesterday
interval:&interval
forDate:[NSDate date]];
yesterday = [yesterday dateByAddingTimeInterval:-interval];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = kCFDateFormatterLongStyle;
df.timeStyle = kCFDateFormatterNoStyle;
df.doesRelativeDateFormatting = YES;
[#[[NSLocale localeWithLocaleIdentifier:#"de_DE"], [NSLocale localeWithLocaleIdentifier:#"es_ES"],[NSLocale localeWithLocaleIdentifier:#"en_US"]] enumerateObjectsUsingBlock:^(NSLocale *locale, NSUInteger idx, BOOL *stop) {
[df setLocale:locale];
[df setTimeZone:[NSTimeZone systemTimeZone]];
NSString *dateString=[df stringFromDate:yesterday];
NSLog(#"%#", dateString);
}];
It outputs correctly
Gestern
ayer
Yesterday
Your problem must lay else-where.
Tested within a command-line program
Make sure that after you changed the language on device/simulator you kill the app completely before you open it again.
Hi I write this Function for LocalTime it Takes input of date as String and covert to Local Time String. May it help you
+(NSString*)LocalTimeWitheDateString:(NSString*)dateString{
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
NSTimeZone *timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
[formatter setTimeZone:timeZone];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *date =[formatter dateFromString:dateString];
NSTimeZone *localTimeZone = [NSTimeZone localTimeZone];
[formatter setTimeZone:localTimeZone];
NSString *LnewTimeZoneDateString = [formatter stringFromDate:date];
return LnewTimeZoneDateString;
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am adding the date and time to my iOS app. I have successfully added the date but want to show the time on a different label.
My code looks like this but I'm getting an error 'Redefinition of dateFormatter'. What can I do to call the time on a separate label?
NSLocale *en_US_POSIX = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
assert(en_US_POSIX != nil);
// The Date
//set the date label correctly
//format the date to July 9 2013 - MMMM d yyyy
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:en_US_POSIX];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSDate *dateFromString = [[NSDate alloc] init];
// dateFromString = [dateFormatter dateFromString:curDate];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MMMM d yyyy"];
NSString *theDate = [dateFormat stringFromDate:dateFromString];
labelDate.text = theDate;
// The Time
NSDate *currentTime = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"hh-mm"];
NSString *resultString = [dateFormatter stringFromDate: currentTime];
labelTime.text = resultString;
Your just need to delete the line where you are reinitializing the NSDateFormatter.
Correct code is :
NSLocale *en_US_POSIX = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
assert(en_US_POSIX != nil);
// The Date
//set the date label correctly
//format the date to July 9 2013 - MMMM d yyyy
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:en_US_POSIX];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSDate *dateFromString = [[NSDate alloc] init];
// dateFromString = [dateFormatter dateFromString:curDate];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MMMM d yyyy"];
NSString *theDate = [dateFormat stringFromDate:dateFromString];
labelDate.text = theDate;
// The Time
NSDate *currentTime = [NSDate date];
[dateFormatter setDateFormat:#"hh-mm"];
NSString *resultString = [dateFormatter stringFromDate: currentTime];
labelTime.text = resultString;
dude just change the NSDateFormatter name :) that's it, your problem solved :)
Try this
NSLocale *en_US_POSIX = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
assert(en_US_POSIX != nil);
//format the date to July 9 2013 - MMMM d yyyy
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:en_US_POSIX];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSDate *dateFromString = [[NSDate alloc] init];
// dateFromString = [dateFormatter dateFromString:curDate];
[dateFormat setDateFormat:#"MMMM d yyyy"];
NSString *theDate = [dateFormat stringFromDate:dateFromString];
labelDate.text = theDate;
// The Time
NSDate *currentTime = [NSDate date];
[dateFormatter setDateFormat:#"hh-mm"];
NSString *resultString = [dateFormatter stringFromDate: currentTime];
labelTime.text = resultString;
I am using the following code to retrieve date and year from NSString. I am printing them in NSLog statements.
NSDate *dateC= [[[NSDate alloc] init] autorelease];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setLocale:[NSLocale currentLocale]];
[dateFormat setDateFormat:#"MM/dd/yyyy HH:mm"];
dateC = [dateFormat dateFromString:#"04/15/2009 00:00"]; // Only this date gives null date.
[dateFormat setDateFormat:#"MMM dd, yyyy"];
NSLog(#"date = %#",[dateFormat stringFromDate:dateC]);
NSCalendar* calendar = [NSCalendar autoupdatingCurrentCalendar];
NSDateComponents* components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:dateC];
NSLog(#"year =%d",[components year]);
[dateFormat release];
This code works fine for every date except one that is mentioned in the code.
The output should be:
date = Apr 15, 2009
year =2009
But the output is:
date = (null)
year =2001
Please help me why am I getting this weird behaviour?
NOTE: I am using Xcode 4.6.2 and iOS 6.1
Please try this exact code. Also note the section under Fixed Formats in the Date Formatters Guide, linked to in the NSDateFormatter class description.
// NSDateFormatter Class Description says:
// Note that although setting a format string (setDateFormat:) in principle specifies an
// exact format, in practice it may nevertheless also be overridden by a user’s
// preferences—see Data Formatting Guide for more details.
NSLocale *locale;
NSLocale *curLocale = [NSLocale currentLocale];
NSLocale *usaLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
if([curLocale.localeIdentifier isEqualToString:usaLocale.localeIdentifier]) {
locale = [[NSLocale alloc] initWithLocaleIdentifier:usaLocale.localeIdentifier];
NSLog(#"Created a new one");
} else {
NSLog(#"USed the old one");
locale = usaLocale;
}
assert(locale);
NSDate *dateC= [[NSDate alloc] init];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setLocale:locale];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
assert(calendar);
[dateFormat setCalendar:calendar];
[dateFormat setDateFormat:#"MM/dd/yyyy HH:mm"];
dateC = [dateFormat dateFromString:#"04/15/2009 00:00"]; // O
NSLog(#"DATE: %#", dateC);
If it is displaying a different hour you may need to set the default timezone (this happens only after ios7)
[dateFormat setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT"]];
Interesting issue thats caught me out.
I receive string times from a server to a device. which I then convert in to a NSDate.
When the device was set to displaying 24hour times, life was good.
Now I am testing it on a device set to 12hour times. everything has stopped working. Dates are coming back as null
I first had
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"HH:mm"];
self.startTime = [dateFormat dateFromString:(NSString *)self.startTime];
Worked perfectly for devices showing 24hour dates but not 12hour.
I then tried
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"hh:mm"];
self.startTime = [dateFormat dateFromString:(NSString *)self.startTime];
This works fine up until 12 noon, then all dates are returned as null
Update
I have also tried adding "a" but this still results in returning null
if (startDate == nil)
{
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"hh:mm a"];
startDate = [dateFormat dateFromString:(NSString *)self.startTime];
}
Update 2
Adding local, adding :ss adding a all still do not work
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
NSLocale *twelveHourLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
dateFormat.locale = twelveHourLocale;
[dateFormat setDateFormat:#"hh:mm a"];
startDate = [dateFormat dateFromString:(NSString *)self.startTime];
It's close... I think you need:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
[dateFormatter setLocale:locale];
[dateFormatter setDateFormat:#"HH:mm"];
startDate = [dateFormatter dateFromString:(NSString *)self.startTime];
You can try this
12 to 24 hour format
NSDateFormatter* df = [[NSDateFormatter alloc] init];
[df setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"]];
[df setTimeZone:[NSTimeZone systemTimeZone]];
[df setDateFormat:#"hh:mm a"];
NSDate* newDate = [df dateFromString:LocationTrackingStartTime];
[df setDateFormat:#"HH:mm"];
newDate = [df stringFromDate:newDate];
24 to 12 hour format
NSDateFormatter* df = [[NSDateFormatter alloc] init];
[df setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"]];
[df setTimeZone:[NSTimeZone systemTimeZone]];
[df setDateFormat:#"yyyy-mm-dd hh:mm:ss"];
NSDate* newDate = [df dateFromString:[df stringFromDate:[NSDate date]]];
[df setDateFormat:#"hh:mm a"];
newDate = [df stringFromDate:[NSDate date]];
You have to add am/pm:
NSLocale *twelveHourLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
dateFormat.locale = twelveHourLocale;
[dateFormat setDateFormat:#"hh:mm: a"];
Change 24 hours format to 12 hour format,
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//Set 24 hours date format
dateFormatter.dateFormat = #"HH:mm";
//Here #"your24hours date string" is your string date with 24 hours format
NSDate *date = [dateFormatter dateFromString:#"your24hours date string"];
//Set 12 hours date format
dateFormatter.dateFormat = #"hh:mm a";
NSString *strDate = [dateFormatter stringFromDate:date];
NSLog (#"%#", strDate);
//Output is in 12 hours date format.
You wanted to convert an NSDate forced to NSString to an NSDate again.
First you have to convert it to and NSString, not forcing it!
I also support 12 and 24 hour formats and i have no problem with it.
NSDateFormatter* df = [[NSDateFormatter alloc]init];
[df setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"]];
[df setTimeZone:[NSTimeZone systemTimeZone]];
[df setDateFormat:#"MM/dd/yyyy hh:mm a"];
NSDate* newDate = [df dateFromString:strDate4Convert];
[df setDateFormat:#"MM/dd/yyyy HH:mm:ss"];
NSString *newTimeStr = [df stringFromDate:newDate];
You just need to set the locale to take the am pm format. And everything else as usual.
NSDateFormatter * df = [[NSDateFormatter alloc]init];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
[df setLocale:locale];
[df setDateStyle:NSDateFormatterNoStyle];
[df setTimeStyle:NSDateFormatterShortStyle];
NSLog(#"%#", [df stringFromDate:pickUpDate]);