I am using this format to get hours
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"hh";
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
NSString *date = [dateFormatter stringFromDate:now];
NSInteger myHour = [date intValue];
The problem is it returns e.g 4Pm i Want it to return 16 Instead of 4. How can I do that? I tried replacing hh by single h by of no avail.
Here Your input:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"hh"];
NSDate *Yourcurrenttime = [dateFormatter dateFromString:Yourstring];
And date converted to the String ..
[dateFormatter setDateFormat:#"HH"];
NSString *date = [dateFormatter stringFromDate:now];
NSInteger myHour = [date intValue];
Here is answer :-
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"HH";
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
NSString *date = [dateFormatter stringFromDate:[NSDate date]];
NSInteger myHour = [date intValue];
NSLog(#"%d",myHour);
Hope it helps you!
Its for 12 hour time format :-
NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setDateFormat:#"hh a"];
NSString *theTime = [timeFormat stringFromDate:[NSDate date]];
NSLog(#"time, %#",theTime);
Its for 24 hour time format:-
NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setDateFormat:#"HH"];
NSString *theTime = [timeFormat stringFromDate:[NSDate date]];
NSLog(#"time, %#",theTime);
Related
I have to convert the given time in 24 hr format.But I did not get it.Don't know where I m going wrong.
If I have TimeStart as 1:15 PM then I should get 13:15 ..
Item *item=[sectionItems objectAtIndex:itemId];
NSLog(#"%#",item.TimeStart);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"HH:mm a"];
NSString *formattedDateString = [dateFormatter stringFromDate:item.TimeStart];
NSLog(#"%#",formattedDateString);
In the code above I have a Item class and that I have NSString *TimeStart;
When I see formattedDateString it is returning null.How can I fix it ?
Item *item=[sectionItems objectAtIndex:itemId];
NSLog(#"%#",item.TimeStart);
NSString *str = #"1:45.PM";// put here item.TimeStart
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"hh:mm.a"];// here give the format which you get in TimeStart
NSDate *date = [dateFormatter dateFromString: str];
dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"HH:mm"];
NSString *convertedString = [dateFormatter stringFromDate:date];
NSLog(#"Converted String : %#",convertedString);
i just simply setDateFormat like [formatter setDateFormat:#"HH:mm"]; Remove a
and below caode for checking its time formate is 24 hr or 12
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setLocale:[NSLocale currentLocale]];
[formatter setDateStyle:NSDateFormatterNoStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
NSString *dateString = [formatter stringFromDate:[NSDate date]];
NSRange amRange = [dateString rangeOfString:[formatter AMSymbol]];
NSRange pmRange = [dateString rangeOfString:[formatter PMSymbol]];
BOOL is24h = (amRange.location == NSNotFound && pmRange.location == NSNotFound);
[formatter release];
NSLog(#"%#\n",(is24h ? #"YES" : #"NO"));
Use this to set 24 hr format.
[currentDate setDateFormat:#"yyyy-MM-dd HH:mm"];
or
[formatter setDateFormat:#"HH:mm"];
without using formatter
NSRange range;
range.location = 2;
range.length = 2;
NSString *dateString=[NSString stringWithFormat:#"%#:%#",[myString substringToIndex:2], [myString substringWithRange:range]];
This is the code i am using in my project and its working for me
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"HH:mm"];
NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];
NSLog(#"Current time date:%#", dateString);
output:Current time date:14:41
I am trying to convert my date from NSString to NSDate using following code
NSDateFormatter *dateformatter = [[NSDateFormatter alloc]init];
[dateformatter setDateStyle:NSDateFormatterShortStyle];
[dateformatter setTimeStyle:NSDateFormatterNoStyle];
[dateformatter setDateFormat:#"dd/MM/yyyy"];
NSDate *myDate = [[NSDate alloc] init];
currMonth = 3;
currYear = 2012;
NSString *str = [NSString stringWithFormat:#"01/%2d/%d", currMonth, currYear];
str = [str stringByReplacingOccurrencesOfString:#" " withString:#"0"];
myDate = [dateformatter dateFromString:str];
NSLog(#"myStr: %#",str);
NSLog(#"myMonth: %2d",currMonth);
NSLog(#"myYear: %d",currYear);
NSLog(#"myDate: %#",myDate);
Above code is giving me wrong date. Can anyone please help?
What is your output? Keep in mind, that NSDate is in UTC.
2012-03-01 00:00:00 (GMT+1) is 2012-02-39 23:00:00 (UTC)
Another tip:
%02 formats your integers with leading zeros.
Try this:
-(NSDate *)dateFromString:(NSString *)string
{
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[dateFormat setLocale:locale];
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSTimeInterval interval = 5 * 60 * 60;
NSDate *date1 = [dateFormat dateFromString:string];
date1 = [date1 dateByAddingTimeInterval:interval];
if(!date1) date1= [NSDate date];
[dateFormat release];
[locale release];
return date1;
}
Tell me if it helps u :]
try
NSDateFormatter *dateformatter = [[NSDateFormatter alloc]init];
[dateformatter setDateStyle:NSDateFormatterShortStyle];
[dateformatter setTimeStyle:NSDateFormatterNoStyle];
[dateformatter setDateFormat:#"dd/MM/yyyy"];
NSDate *myDate = [[NSDate alloc] init];
int currMonth = 3;
int currYear = 2012;
NSString *str = [NSString stringWithFormat:#"01/%2d/%d", currMonth, currYear];
str = [str stringByReplacingOccurrencesOfString:#" " withString:#"0"];
//SET YOUT TIMEZONE HERE
dateformatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:#"PDT"];
myDate = [dateformatter dateFromString:str];
NSLog(#"myStr: %#",str);
NSLog(#"myMonth: %2d",currMonth);
NSLog(#"myYear: %d",currYear);
NSLog(#"myDate: %#",myDate);
I have a string that contain date:
2012-05-25
and i wan to convert this string to NSDate:
NSString *birthday = /*2012-05-25*/;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"YYYY-MM-DD"];
NSDate *dateFromString = [dateFormatter dateFromString:birthday];
and in the dateFromString i get the date of today.
This works :
NSString *birthday = #"2012-05-25";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSDate *dateFromString = [dateFormatter dateFromString:birthday];
NSLog(#"%#", dateFromString);
Outputs :
2012-01-25 11:36:38.157 dsv[2679:903] 2012-05-25 00:00:00 +0200
Please refer to the Apple guide for details.
I hope this helps u. try this
- (NSDate*) dateFromString:(NSString*)aStr
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"] autorelease]];
[dateFormatter setDateFormat:#"MM/dd/yyyy HH:mm:ss a"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSLog(#"%#", aStr);
NSDate *aDate = [dateFormatter dateFromString:aStr];
[dateFormatter release];
return aDate;
}
I have struggling with to convert nsstring to nsdate format. I have a date in nsstring format like (2011-08-25 18:30:00 +0000), but i want to change it in nsdate format (08/26/2011). How to change it? Can any one help me please? Thanks in advance... I have spend one day to solve this but i cant find out the solution.. Please help me friends....
This is my code,
NSString *dateString = #"2011-08-25 18:30:00 +0000";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM/dd/yyyy"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
NSLog(#"Converted date is %#",dateFromString);
Output is,
Converted date is (null)
Yuva.M
NSString *dateWithInitialFormat = #"2011-08-25 18:30:00 +0000";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];
NSDate *date = [dateFormatter dateFromString:dateWithInitialFormat];
[dateFormatter setDateFormat:#"MM/dd/yyyy"];
NSString *dateWithNewFormat = [dateFormatter stringFromDate:date];
NSLog(#"dateWithNewFormat: %#", dateWithNewFormat);
From NSLog: dateWithNewFormat: 08/25/2011
See Date Format Patterns
You need to use an NSDateFormatter and set the date format to match how you're storing it, like the below.
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
NSDate *myDate = [df dateFromString: myDateString];
NSDateFormatter* df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"MMMM dd, yyyy"];
NSDate* d = [df dateFromString:#"January 06, 1992"];
NSLog(#"%#", d);
NSDate *dispalyDate = d;
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd MMM"];
NSString *str = [dateFormat stringFromDate:dispalyDate];
NSLog(#"%#", str);
I am using the below code:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterFullStyle];
NSDate *today=[NSDate date];
NSString *string = [dateFormatter stringFromDate:today];
NSLog(#"%#",string);
[dateFormatter release];
This will not give me this format 03/20/11 11:24 AM(MONTH/DATE/YEAR HOUR:MIN), if i convert system date and time so the converted string should have such type of information 03/20/11 11:24 AM(MONTH/DATE/YEAR HOUR:MIN).? What should i do?
you should set the date format as,
[dateFormat setDateFormat:#"dd/MM/YYYY hh:mm a"];
Try doing this and u will get the format
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MM/dd/yy HH:mm"];
NSDate *today=[NSDate date];
NSString *string = [formatter stringFromDate:today];
NSLog(#"%#",string);
[formatter release];
Good Luck!