I am very new in iPhone. I have one issue. First Just check my code.
NSString *timeZone=[NSString stringWithFormat:#"%#",[[NSTimeZone systemTimeZone] abbreviation]];
NSLog(#"Print Time Zone :- '%#'",timeZone);
NSRange Timerange=[timeZone rangeOfString:#"+"];
NSString *time;
if(Timerange.location == NSNotFound)
{
Timerange=[timeZone rangeOfString:#"-"];
time=[timeZone substringFromIndex:Timerange.location];
NSLog(#"Time is :- '%#'",time);
time=[time stringByReplacingOccurrencesOfString:#"-" withString:#"M"];
}
else
{
Timerange=[timeZone rangeOfString:#"+"];
time=[timeZone substringFromIndex:Timerange.location];
NSLog(#"Time is :- '%#'",time);
time=[time stringByReplacingOccurrencesOfString:#"+" withString:#"P"];
}
Its working proper in iPhone Simulator 4.2 but it crashes in simulator 4.3 so what can be the issue?
Thanks
NSDate *date=[NSDate date];
NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
[formatter setDateFormat:#"MM/dd/yyyy hh:mm:ss a"];
[formatter setTimeZone:[NSTimeZone systemTimeZone]];
[formatter setTimeStyle:NSDateFormatterLongStyle];
NSString *dateOfGame =[formatter stringFromDate:date];
NSString *timeZone = [dateOfGame substringFromIndex:12];
NSLog(#"dateOfGame%#",timeZone);
try with this code
you might need coded like this
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"] autorelease]];
[dateFormatter setDateFormat:#"z"];
NSDate *date = [NSDate date];
NSString *dateString = [dateFormatter stringFromDate:date];
Related
I have application in which i am using NSDate and saving it in NSString but NSString store 000.000 like this here is the code which i am using
NSDate* date = [NSDate date];
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd HH:MM:SS"];
NSString* str = [formatter stringFromDate:date];
NSString*content_AddedTime=str;
NSString *post =[[NSString alloc] init];
post = [NSString stringWithFormat:#"userId=%#&catalogID=%#&content_AddedTime=%#&organizationCode=%#",userId,catalogID,content_AddedTime,organizationCode];
Try this:
[formatter setDateFormat:#"yyyy/MM/dd HH-mm-ss"];
or you can use:
[formatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss ZZZ"];
Hope it will help!
Try to Log the NSString.There is no issue with your code.
NSDate* date = [NSDate date];
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd HH:MM:SS"];
NSString* str = [formatter stringFromDate:date];
NSString*content_AddedTime=str;
NSLog(#"%#",str);
NSLog(#"%#",content_AddedTime);
OutPut:
2013-07-30 12:07:67
2013-07-30 12:07:67
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"MMMM/dd/yyyy"];//set ur format
NSDate *date = [NSDate date];
NSString *str= [dateFormatter stringFromDate:date];
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 converting string to date and again to string. using following code
NSString *newDate =[NSString stringWithFormat:#"%#",[(Jobs *)[tableArray objectAtIndex:indexPath.row]Date]];
NSLog(#"New Date is %#",newDate);
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MM/dd/yyyy h:mm:ss a"];
NSDate *myDate = [dateFormat dateFromString:newDate];
[dateFormat setDateFormat:#"MM/dd/yyyy"];
NSString *str = [dateFormat stringFromDate:myDate];
NSLog(#"DAte is %#",str);
It is returning date in simulator but not in iphone 4.3.3
please help
NSDateFormatter will return nil if it can't get the date from a string.
To see if it can parse the date add an other NSLog statement:
NSString *newDate =[NSString stringWithFormat:#"%#",[(Jobs *)[tableArray objectAtIndex:indexPath.row]Date]];
NSLog(#"New Date is %#",newDate);
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MM/dd/yyyy h:mm:ss a"];
NSDate *myDate = [dateFormat dateFromString:newDate];
NSLog(#"Date parsed: %#", myDate);
[dateFormat setDateFormat:#"MM/dd/yyyy"];
NSString *str = [dateFormat stringFromDate:myDate];
NSLog(#"DAte is %#",str);
But since you only want the firs part of the string why not just substring it.
NSString *str = [newDate substringToIndex:10];
and what is returned here:
NSString *newDate =[NSString stringWithFormat:#"%#",[(Jobs *)[tableArray objectAtIndex:indexPath.row]Date]];
it the property Date already a NSDate ?, can you show your .h file for Jobs?
converting NSDate to NSString creates a memory leak can anyone help.
Here is my code:-
NSDate *today = [[NSDate alloc]init];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd"];
dateString = nil;
dateString =[[NSString alloc]initWithString:[df stringFromDate:today]];
[df setDateFormat:#"EEEE MMM dd yyyy"];
[dateButton setTitle:[df stringFromDate:today] forState:UIControlStateNormal];
[df release];
[today release];
As you aren't releasing anything the code creates a memory leak.
NSDate *today = [NSDate date]; //Autorelease
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease]; //Autorelease
[df setDateFormat:#"yyyy-MM-dd"]; // 2017-09-28
dateString = [[df stringFromDate:today] retain];
[df setDateFormat:#"EEEE MMM dd yyyy"]; // Thursday Sep 28 2017
[dateButton setTitle:[df stringFromDate:today] forState:UIControlStateNormal];
For more details, you can refer to Apple's documentation.
Use
NSDate *today = [NSDate date];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd"];
dateString = [df stringFromDate:today];
[df release]
Using your code...
NSDate *today = [[NSDate alloc]init];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd"];
dateString = nil;
dateString = [[NSString alloc]initWithString:[df stringFromDate:today]];
...you need to release a lot of obj because nothing is in autorelease.
[today release]; -> alloc
[df release] -> alloc
[dateString release]; -> alloc
Or change to:
NSDate *today = [NSDate date];
NSDateFormatter *df = [NSDateFormatter initWithDateFormat:#"yyyy-MM-dd"];
dateString = [df stringFromDate:today];
with no one release/alloc!
The leak is in the DateFormatter that is not being released.
This should fix the leak :
[df release];
Also, try using ...
[NSDate date]
instead of ...
NSDate* today = [[NSDate alloc] init];
that is a lot of alloc/initing that you are doing there as well... you don't need to alloc/init the NSString either.
You Shoud noy alloc or init NSDate object
Try this Code
NSDate *today = [NSDate date];
NSDateFormatter *dt = [[NSDateFormatter alloc]init];
[dt setDateFormat:#"yyyy-mm-dd"];
NSString *str =[NSString stringWithFormat:#"%#",[dt stringFromDate:today]];
NSLog(#"%#",str);
[dt release];
Happy Coding
Yet another response:
NSDateFormatter *df = [NSDateFormatter initWithDateFormat:#"yyyy-MM-dd"];
NSString *dateString = [df stringFromDate:[NSDate date]];
using ARC, all autoreleased.
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!