When I want a date in 24 hour format, I see the hours such as 05:00. How can I delete the first 0 to get 5:00? I tried this:
NSDateFormatter *fhours = [[NSDateFormatter alloc] init];
[fhours setTimeStyle:NSDateFormatterShortStyle];
mystring = [fhours stringFromDate:newDate];
[fhours release];
Like this
NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setDateFormat:#"HH:mm"];
NSDate *now = [NSDate date];
NSString *theTime = [timeFormat stringFromDate:now];
At line
[timeFormat setDateFormat:#"HH:mm"];
You can erase one H like this
[timeFormat setDateFormat:#"H:mm"];
You should get the right result with the following:
NSDateFormatter *fhours = [[NSDateFormatter alloc] init];
[fhours setDateFormat:#"H:mm"];
mystring = [fhours stringFromDate:newDate];
[fhours release];
Related
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);
i have a date time like this
2012-01-01 10:00:00
i need to convert it into date only like this
2012-01-01
Can anyone help me please
this is my code
NSDate *adate = objIStruct_Conference.m_DtDate;
NSLog(#"DATE========%#",objIStruct_Conference.m_DtDate);
NSString *DateString = [NSString stringWithFormat:#"%#", adate ];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"] autorelease]];
[formatter setDateFormat:#"yyyy:MM:dd"];
NSLog(#"NEW DATE====%#", [formatter stringFromDate:adate]);
[formatter release];
i am getting DATE===== 2012-01-01 10:00:00 , NEW DATE=====(null)
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy:MM:dd"];
NSLog(#"%#", [formatter stringFromDate:[NSDate date]]);
[formatter release];
See the Class Reference of NSDateFormatter
Please use this code:
NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init] autorelease];
[dateFormat setDateFormat:#"yyyy-MM-dd"];
NSLog(#"%#",[dateFormat stringFromDate:date]);
it seems your adate pointer would be nil, because it is working perfectly for me on the real device with a valid NSDate but when I set the _date to nil, the output will be nil as in your case.
NSDate *_date = [NSDate date];
NSDateFormatter *_dateFormatter = [[NSDateFormatter alloc] init];
[_dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"]];
[_dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSLog(#"%#", [_dateFormatter stringFromDate:_date]);
the output is:
2012-08-08
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 able to fetch NSString representing the current date and time in PST using the following code.
//get current date and time
NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:#"America/Los_Angeles"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM/dd/yy HH:mm:ss zzz"];
[dateFormatter setTimeZone:timeZone];
NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
[dateFormatter setLocale:enUSPOSIXLocale];
[enUSPOSIXLocale release];
NSDate *date = [NSDate date];
NSString *currentDateStr = [dateFormatter stringFromDate:date];
NSLog(#"currentDateStr %#",currentDateStr);
[dateFormatter release];
EDIT:
But the following code to fetch NSDate from NSString doesn't serve my purpose.
//get date from string
NSDateFormatter* formatter2 = [[NSDateFormatter alloc] init];
[formatter2 setDateFormat:#"MM/dd/yy HH:mm:ss"];
NSDate *dt = [[NSDate alloc]init];
dt = [formatter2 dateFromString:currentDateStr];
NSLog(#"dt %#",dt);
"dt" appears to be null.
Could anybody help me out in solving this issue of getting NSDate from NSString.
Thanx in advance.
NSDateFormatter also has dateFromString: function.
EDIT:
//get date from string.
NSDateFormatter* formatter2 = [[NSDateFormatter alloc] init];
[formatter2 setDateFormat:#"MM/dd/yy HH:mm:ss zzz"];
NSDate *dt = [formatter2 dateFromString:currentDateStr];
NSLog(#"dt %#",dt);
Make use of
[dateFormatter dateFromString:urString];
In your case urString is currentDateStr
Use
- (NSString *)datePickerValueChangedd:(UIDatePicker*) datePicker{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 50, 68, 68)];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = NSDateFormatterMediumStyle;
label.text = [NSString stringWithFormat:#"%#",[df stringFromDate:datePicker.date]];
NSLog(#"I am inside the datePickerValueChanged - - %#", label.text);
[df release];
globalVariable= label.text;
return (label.text);
}
I'm trying to take a NSString date in the format "2/22/11" and convert it to this format: 2011-02-22
This is my code:
NSDate *dateTemp = [[NSDate alloc] init];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd"];
dateTemp = [dateFormat dateFromString:newInvoice.date];
newInvoice.date = [dateFormat stringFromDate:dateTemp];
newInvoice.date starts as an NSString equal to "2/22/11". dateTemp ends up NIL. newInvoice.date ends up NIL as well.
I can't for the life of me figure out why.
You are facing this problem because your date formatter is not correct.Suppose your newInvoice.date variable store "11:02:23" the your dateFormatter should be #"yy:MM:dd" and if your newInvoice.date variable store"2/22/11" then your dateFormatter should be #"MM/dd/yy"
NSDate *dateTemp = [[NSDate alloc] init];
NSDateFormatter *dateFormat1 = [[NSDateFormatter alloc] init];
NSDateFormatter *dateFormat2 = [[NSDateFormatter alloc] init];
[dateFormat1 setDateFormat:#"MM/dd/yy"];
[dateFormat2 setDateFormat:#"yyyy-MM-dd"];
dateTemp = [dateFormat1 dateFromString:newInvoice.date];
newInvoice.date = [dateFormat2 stringFromDate:dateTemp];
both format should be according to your requirement to get the correct result
This should work fine. I have set Date style. You can change NSDateFormatterShortStyle to something else. Also check your newInvoice.date format.
NSDate *dateTemp = [[NSDate alloc] init];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateStyle: NSDateFormatterShortStyle];
dateTemp = [dateFormat dateFromString: newInvoice.date];
[dateFormat setDateFormat:#"yyyy-MM-dd"];
newInvoice.date = [dateFormat stringFromDate:dateTemp];