I am writing a method that takes string and returns string. I am trying to use it in another method.
- (NSString*) formatDate:(NSString*) showStartDateTime
{
NSTimeZone *tz = [NSTimeZone timeZoneWithName:#"UTC"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:tz];
[dateFormatter setDateFormat:#"EEE MM/dd/yyyy"];
NSDate*dattt=[dateFormatter dateFromString:showStartDateTime ];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSString*tempo2= [dateFormatter stringFromDate:dattt];
return tempo2;
}
This is how I am using it. [self formatDate:#"Fri 08/08/2014"] is it a correct way to use it?
NSString *hellostring=[self formatDate:#"Fri 08/08/2014"];
NSLog(#"%#",hellostring);
I know it does not work. Any suggestion is appreciated.
I wrote a command line app and wasnt able to get it running, as already dateFromString: resulted in nil, until I added dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
- (NSString*) formatDate:(NSString*) showStartDateTime
{
NSTimeZone *tz = [NSTimeZone timeZoneWithName:#"UTC"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:tz];
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
[dateFormatter setDateFormat:#"EEE MM/dd/yyyy"];
NSDate*dattt=[dateFormatter dateFromString:showStartDateTime ];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSString*tempo2= [dateFormatter stringFromDate:dattt];
return tempo2;
}
Now it returns 2014-08-08
see Technical Q&A QA1480: NSDateFormatter and Internet Dates
Related
I want to use date format like this 2015-08-14 but unfortunately I am getting this format 16/11/15
Below is the code I have used to get desired output.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter setTimeZone:gmt];
[dateFormatter setDateStyle:kCFDateFormatterShortStyle];
NSString *formattedDateString = [dateFormatter stringFromDate:selectedDate];
Can anyone please suggest how to fix this issue?
Just remove the line [dateFormatter setDateStyle:NSDateFormatterShortStyle];
-(NSString*)stringFromDateWithFormat:(NSString*)formatString{
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
dateFormatter.dateFormat = formatString;
return [dateFormatter stringFromDate:self];
}
NSDate *currDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"YYYY-MM-dd"];
NSString *dateString = [dateFormatter stringFromDate:currDate];
NSLog(#"date is %#",dateString);
NSDate *selectedDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter setTimeZone:gmt];
NSString *formattedDateString = [dateFormatter stringFromDate:selectedDate];
NSLog(#"formattedDateString = %#", formattedDateString); // Prints 2015-10-15
I am using following code to get a date from string but its not showing correct date
NSDateFormatter *saveddf = [[NSDateFormatter alloc] init];
[saveddf setDateFormat:#"EEEE_MMMM dd_YYYY hh:mm a"];
[saveddf setTimeZone:[NSTimeZone systemTimeZone]];
NSDate *savedtdate = [[NSDate alloc] init];
savedtdate = [saveddf dateFromString:datestring];
Saturday_June 22_2013 11:44 AM -- INPUT (datestring).
2013-01-05 06:14:00 +0000 This is output i am getting.(savedtdate).
Please tell whats wrong in my code.
Try this code, it should work:
NSString *yourStringDate = #"Saturday_June 22_2013 11:44 AM";
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"EEE_MMM dd_yyyy hh:mm a"];
NSDate *date = [dateFormatter dateFromString: yourStringDate];
//Set the required date format
[dateFormatter setDateFormat:#"dd-MM-yyyy hh:mm"];
NSString *convertedString = [dateFormatter stringFromDate:date];
NSLog(#"Converted String : %#",convertedString);
Try to use this code
NSDateFormatter *formatter;
NSString *dateString;
formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd-MM-yyyy HH:mm"];
dateString = [formatter stringFromDate:[NSDate date]];
NSLog(#"%#",dateString);
I have this method on an object:
- (NSString *)formattedScheduledOn
{
NSDateFormatter *dateFormater = [[NSDateFormatter alloc] init];
[dateFormater setDateFormat:#"MM/dd/yyyy hh:mm"];
return [dateFormater stringFromDate:self.scheduledOn];
}
scheduledOn is an NSDate object. Showing it as a raw date yields something like: 2013-03-22T09:44:28-07:00
But I always get null from that method. Any ideas? Thanks!
Make sure your date is not nil.
Try the following code and see what you get in return
NSDateFormatter *dateFormater = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
[dateFormater setDateFormat:#"MM/dd/yyyy hh:mm"];
return [dateFormater stringFromDate:[NSDate date]];
If change you passing the date as string then you should set the format according to the date string passed.
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssz"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
Why not do something like this?
Set your datePicker to pick or whatever you want.
[pick addTarget:self action:#selector(updateDateLabel:) forControlEvents:UIControlEventValueChanged];
-(IBAction)updateDateLabel:(id)sender {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterLongStyle];
[formatter setTimeStyle:NSDateFormatterMediumStyle];
dateLabel.text = [formatter stringFromDate:pick.date];
}
When I profile my iOS application, I found too much Memory leaks:
There is my code with NSDateFormatter and the code is in one loop:
for (NSDictionary * dataDict in deserializedData) {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone localTimeZone];
[dateFormatter setTimeZone:timeZone];
[dateFormatter setDateFormat : #"yyyy-MM-dd HH:mm:ss"];
NSString *currentDateString = [dateFormatter stringFromDate:[NSDate date]];
[dateFormatter release];
}
Who can tell me what's wrong with my code.
Just try with auto release like this,
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
NSTimeZone *timeZone = [NSTimeZone localTimeZone];
[dateFormatter setTimeZone:timeZone];
[dateFormatter setDateFormat : #"yyyy-MM-dd HH:mm:ss"];
for (NSDictionary * dataDict in deserializedData) {
NSString *currentDateString = [dateFormatter stringFromDate:[NSDate date]];
}
There is nothing wrong in this code. But I think it is not called on main thread.
Just create an autorelease pool on the beginning of the function in which you have written this code. At the end of the function release the pool.
-(void) yourFun
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
//other stuff...
for (NSDictionary * dataDict in deserializedData) {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone localTimeZone];
[dateFormatter setTimeZone:timeZone];
[dateFormatter setDateFormat : #"yyyy-MM-dd HH:mm:ss"];
NSString *currentDateString = [dateFormatter stringFromDate:[NSDate date]];
[dateFormatter release];
}
[pool release];
}
I have the current time format returned from the web service:
2012-06-25T23:45:52.664Z
I want to get something like:
2012 june 25 23:45
So without seconds and the Z format indicator.
My relevant code is:
myConvertedTime = [[myActualTime dateFromString] stringFromDate];
try these
NSDate *dateTemp = [[NSDate alloc] init];
NSDateFormatter *dateFormat1 = [[NSDateFormatter alloc] init];
[dateFormat1 setDateFormat:#"yyyy MMM dd hh:mm"];
dateTemp = [dateFormat1 dateFromString:newInvoice.date];
You should use an NSDateFormatter object. And rather than specify a format yourself, it's best to let the framework pick the one that suits based on the user's internationalization settings. This is done as follows:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterLongStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
myConvertedTime = [formatter stringForObjectValue:[myActualTime dateFromString]];
[formatter release];
try this:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy MMMM dd HH:mm"];
NSString *myConvertedTime = [formatter stringFromDate:now];
Try the following code :
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss"];
NSString *dateString = #"2012-06-25T23:45:52.664Z";
dateString = [dateString substringToIndex:[dateString length]-5];
NSDate *date = [dateFormatter dateFromString:dateString];
[dateFormatter release];
NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc] init];
[dateFormatter2 setDateFormat:#"yyyy MMMM dd HH:mm"];
NSString *dateText = [dateFormatter2 stringFromDate:date];
[dateFormatter2 release];
NSLog(#"the date is:%#",dateText);