I have to send a DDMMYYY date from a Date to communicate with an API.
NSDateComponents *dateComponents;
NSCalendar *gregorian;
NSDate *date;
gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
dateComponents = [[NSDateComponents alloc] init];
self.date = [gregorian dateByAddingComponents:dateComponents toDate:[NSDate date] options:0];
Do I have to use the [dateComponents day] and so on? is there some method that can help me to performing that task like in PHP?
Try this
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"ddMMyyyy"];
NSString *textDate = [NSString stringWithFormat:#"%#",[dateFormatter stringFromDate:[NSDate date]]];
NSLog(#"Date %#",textDate);
[dateFormatter release];
Just:
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:#"ddMMyyyy"];
NSString *textDate = [NSString stringWithFormat:#"%#",[outputFormatter stringFromDate:[NSDate date]]];
[outputFormatter release];
Try this code:
NSDateFormatter* dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"] autorelease]];
[dateFormatter setDateFormat:#"ddMMyyyy"];
NSDate *current = [NSDate date];
NSString *currentstring = [dateFormatter stringFromDate:current]
check the iphonetips,stackoverflow,apple reference
NSDate *date=[NSDate date];
NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
[formatter setDateFormat:#"ddMMyyyy"];
NSString *dateOfGame =[formatter stringFromDate:date];
[formatter release];
Try this:
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"ddMMyyyy"];
NSLog(#"%#", [dateFormatter stringFromDate:[NSDate date]]);
Agree With Ruben Esposito
Just change your code like this.
NSDateFormatter *dtFormatter = [[NSDateFormatter alloc]init];
[dtFormatter setDateFormat:#"ddMMyyyy"];
NSString *strDate = [NSString stringWithFormat:#"%#",[dtFormatter stringFromDate:[NSDate date]]];
[dtFormatter release];
NSLog(#"%#",strDate);
Related
I try to convert NSString to NSDate with this code:
NSString *dateString=[[NSString alloc] init];
dateString = [[messageArray objectAtIndex:i] tim_dte];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"HH:mm,yyyy/MM/dd"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
msg.date = dateFromString;
But msg.Date is nil even if dateString shows the correct date string. What am I missing?
change this format
[dateFormatter setDateFormat:#"HH:mm,yyyy/MM/dd"];
into and try
[dateFormatter setDateFormat:#"yyyy/MM/dd HH:mm"];
Update
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy/MM/dd HH:mm"];
// use simple
msg.date = [dateFormatter dateFromString:#"2016/05/06 12:36"];
NSLog(#"final date == %#",dateFromString);
//NSDate *dateFromString = [dateFormatter dateFromString:#"2016/05/06 12:36"];
// NSLog(#"final date == %#",dateFromString);
Try this code to convert NSString to NSDate
NSString *dateString = #"2016-05-06";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *date = [dateFormatter dateFromString:#"2013-02-01T06:25:47Z"];
NSTimeZone *pdt = [NSTimeZone timeZoneWithAbbreviation:#"PDT"];
[dateFormatter setTimeZone:pdt];
[dateFormatter setDateFormat:#"HH:mm:ss zzz"];
[dateFormatter setDateFormat:#"K:mm a, z"];
NSString * updated String = [dateFormatter stringFromDate:date];
Use this Code,
NSString *dateString=[[NSString alloc] init];
dateString =[[messageArray objectAtIndex:i] tim_dte];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy/MM/dd HH:mm"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
msg.date=dateFromString;
hope its helpful
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
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);
I receive from the server a string with a date that looks like this: "2013-03-29T15:27:00Z". How can I format it and transform it into an NSDate?
Try this:
[NSDateFormatter setDefaultFormatterBehavior:NSDateFormatterBehavior10_4];
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *date = [formatter dateFromString:#"2013-03-29T15:27:00Z"];
NSString *departTimeDate = #"2013-03-29T15:27:00Z";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];
NSDate *date = [dateFormatter dateFromString:departTimeDate];
NSLog(#"Expected Result___ %#",date);
NSString *string=#"2013-03-29T15:27:00Z";
NSDateFormatter *dateFormatter=[NSDateFormatter new];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSDate *date=[dateFormatter dateFromString:string];
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];
}