How to get current date to particular format - ios

I am new to iOS.
I got wrong format when getting NSLog for date.
Output is 2013-12-06 18:30:00 +0000 but I need to display in dd/mm/yyyy format.
Here is my code:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSLog(#"%#", [dateFormatter stringFromDate:[NSDate date]]);
[dateFormatter setDateFormat:#"dd/MM/yyyy"];
NSLog(#"%#", [dateFormatter stringFromDate:[NSDate date]]);
NSString *str=[dateFormatter stringFromDate:[NSDate date]];
[dateFormatter setDateFormat:#"dd/MM/yyyy"];
NSDate *date=[dateFormatter dateFromString:[dateFormatter stringFromDate:[NSDate date]]];
[dateFormatter release];

Don't make it complex,this should work.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd/MM/yyyy"];
NSLog(#"%#", [dateFormatter stringFromDate:[NSDate date]]);

May this help you.
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:#"dd/MMM/yyyy"];
NSDate *now = [[NSDate alloc] init];
NSString *dateString = [format stringFromDate:now];
NSLog(#"%#",dateString);
Enjoy coding

NSDate *date = [NSDate date];
NSLog(#"%#",date);
Above code will give you following output:
2014-11-27 10:44:43 +0000
But whenever you want to set date in proper format by removing time then follow the below code,
NSDate *date = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd/MM/yyyy"];
NSString *dateString = [dateFormat stringFromDate:date];
NSLog(#"%#",dateString);
The Output will be : 27/11/2014

Related

NSDateFormatter dateFromString with format 'hh:mm a' return nil

i came across a problem in my iPhone application.
in my app i converte time string to date like that
NSString *time = #"01:30 PM";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"hh:mm a"];
NSDate *date = [dateFormat dateFromString:time];
and for some reason always date is nil after [dateFormat dateFromString:time].
can anyone tell me what i'm doing wrong?
thanks
I'm assuming your device language is other then English. In that case you need to set the date formatter Locale. Try this code.
NSString *time = #"01:30 PM";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"hh:mm a"];
[dateFormat setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"]];
try this...
NSString *time = #"01:30 pm";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setTimeStyle:NSDateFormatterShortStyle];
[dateFormat setDateFormat:#"hh:mm a"];
dateFormat.timeZone = [NSTimeZone timeZoneWithName:#"GMT"];
NSDate *date = [dateFormat dateFromString:time];
NSLog(#"date %#",[dateFormat stringFromDate:date]);
NSString *dats1 = #"01:30:00 PM";
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init] ;
[dateFormatter1 setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter1 setDateFormat:#"HH:mm:ss"];
NSDate *date = [dateFormatter1 dateFromString:dats1];
NSLog(#"date : %#", date);
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"hh:mm a"];
NSLog(#"Current Date: %#", [formatter stringFromDate:date]);

How to convert datetime string into NSDate in iOS?

I have datetime format like this
2015-10-06 12:39:12
when i try to convert into nsdate it return me null. i am using below code for it.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];
NSDate *date = [formatter dateFromString:user_post.modification_date];
Please tell me how can i convert it into correct NSDate?
dateString is your date in string format.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
Try to set like this
NSDate *currDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"YYYY-MM-dd HH:mm:ss"];
NSString *dateString = [dateFormatter stringFromDate:currDate];
Change this line only.
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
Z is the timeZone
I suggest you to set the timezone, can be the cause of the problem
[formatter setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
[formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"]];

How to display date format like “16 Mar,Monday” in iOS? 2

I need to display today date format like 16 Mar,Monday,Here my code is
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd MMM, EEEE"];
dateLabel.text = [dateFormat stringFromDate:[NSDate date]];
I am getting output is 16MAR,MONDAY ,But my output should be 16 Mar, Monday.Thanks
Try this
NSDate *date = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd MMM,EEEE"];
NSString *dateString = [dateFormatter stringFromDate:date];
NSLog(#"Date String %#",dateString);
I am getting same output that you want
Hope you will got success..

invalid date in NSDateFormatter

self.dateFormatter = [[NSDateFormatter alloc] init];
[self.dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
[self.dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"]];
[self.dateFormatter setDateFormat:#"yyyyMMdd"];
NSDate *date = [NSDate date];
NSString *dateString = [self.dateFormatter stringFromDate:date];
this code sometimes returns for me incorrect date such as 201300090005.
I don't understand why.
I tried the something like this, but it always works fine for me:-
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init]autorelease];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"]];
[dateFormatter setDateFormat:#"yyyyMMdd"];
NSDate *date = [NSDate date];
NSString *dateString = [dateFormatter stringFromDate:date];
NSLog(#"yourDateString=%#",dateString);

Not getting correct date from string

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);

Resources