I want to convert a string to a date time object and then subtract the difference between 2 dates. However I keep getting an invalid pointer warning. The date is formatted 20131209 02:34. My code is below
NSDateFormatter *df =[[NSDateFormatter alloc]init];
[df setDateFormat:#"YYYYMMDD HH:MM"];
NSDate *currTime = [df dateFromString:self.currentTime];
NSDate *predicTime =[df dateFromString:self.predictedTime];
NSTimeInterval newtime=[predicTime timeIntervalSinceDate:currTime];
Your date format was incorrect [df setDateFormat:#"YYYYMMDD HH:MM"]; I assumed that your strings are valid !
Try :
NSDateFormatter *df =[[NSDateFormatter alloc]init];
[df setDateFormat:#"yyyyMMdd HH:mm"]; // 24hr format, for 12hr format use 'hh'
NSDate *currTime = [df dateFromString:#"20131209 02:34"];
NSDate *predicTime =[df dateFromString:#"20131209 04:34"];
NSTimeInterval newtime=[predicTime timeIntervalSinceDate:currTime];
try out this code
NSDateFormatter *df =[[NSDateFormatter alloc]init];
[df setDateFormat:#"yyyyMMdd hh:mm"];
NSDate *currTime = [df dateFromString:self.currentTime];
NSDate *predicTime =[df dateFromString:self.predictedTime];
NSTimeInterval newtime=[predicTime timeIntervalSinceDate:currTime];
Related
NSDateFormatter dateFromString fetched older date.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSDate *date = [dateformatter dateFromString:#"2015-05-18"];
The above function returns date as 2015-05-17 11:39:18 +0000 instead of
2015-05-18
Actually you are printing NSDate, if you are printing Date then compiler automatically convert your date to string by converting using current timezone. if you want actual converted date in NSString form then add one more line that convert NSDate to NSString like as bellowed.
NSDateFormatter * dFormatter = [[NSDateFormatter alloc] init];
[dFormatter setDateFormat:#"yyyy-MM-dd"];
NSDate *date = [dFormatter dateFromString:#"2015-05-18"];
NSLog(#"String : %#",[dFormatter stringFromDate:date]);
Output :
2015-05-18
this is working code to show the current date...might be solve your problem..you should go for this...
NSDate *Date = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSString *dateFormated = [dateFormatter stringFromDate:Date];
NSLog(#"%#", dateFormated);
I am getting an value of 2015-04-22 10:43:57 from server but getting problem when converting into NSDate. I'd like to convert the same into NSDate into same format given in NSString. Here is my code
// getting from server
NSString *dateString=#"2015-04-22 10:43:57";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *aDate = [dateFormat dateFromString:dateString];
If I will get dateString with timezone then will it be resolved.
Your code looks correct.
I imagine you are having a problem with timezones while formatting. You culd addnit to your NSDateFormatter to specify if input date is from an specific timeZone
// set date format
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
dateFormatter.timeZone = [NSTimeZone timeZoneWithName:#"UTC"];
Please use below code
NSString *dateString=#"2015-04-22 10:43:57";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSTimeZone* localTimeZone = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
dateFormat.timeZone=localTimeZone;
NSDate *aDate = [dateFormat dateFromString:dateString];
OutPut: 2015-04-22 10:43:57 +0000
Hope it helps you..!
Your code is correct. You just try adding this line to your code.
//This will set time zone of your device.You can change systemTimeZone to localTimeZone based on your need.
[dateFormat setTimeZone:[NSTimeZone systemTimeZone]];
My date string that I am getting is 2014-01-08T21:21:22.737+05:30 of this format. How do i confer it to NSDate?
I tried:
NSString *currentDateString = #"2014-01-08T21:21:22.737+05:30";
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS+05:30"];
NSDate *currentDate = [dateFormatter dateFromString:currentDateString];
NSLog(#"CurrentDate:%#", currentDate);
It returns nil. Any thoughts?
Use this code it is working fine your dateFormatter was wrong .
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];
NSString *currentDateString = #"2014-01-08T21:21:22.737+05:30";
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZ"];
NSDate *currentDate = [dateFormatter dateFromString:currentDateString];
NSLog(#"CurrentDate:%#", currentDate);
Happy coding!!!!!!!!!!.
Your date format contains a time zone as a literal: yyyy-MM-dd'T'HH:mm:ss.SSS+05:30 the +05:30 it the time zone definition. You should parse it with Z.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
NSString *currentDateString = #"2014-01-08T21:21:22.737+05:30";
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZ"];
NSDate *currentDate = [dateFormatter dateFromString:currentDateString];
NSLog(#"CurrentDate:%#", currentDate);
NSString *p=#"2014-01-08T21:21:22.737+05:30";
NSDateFormatter *dateformat=[[NSDateFormatter alloc]init];
[dateformat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZ"];
NSDate *datefor=[dateformat dateFromString:p];
[dateformat setDateFormat:#"MM-dd-yyyy hh:mm a"];
NSString *dateStr=[dateformat stringFromDate:datefor];
NSDate *datetype=[dateformat dateFromString:dateStr];
I think you need to initialize the dateFormatter like this below in your code. Because if do not initialze always you will get the null value:-
dateFormatter=[[NSDateFormatter alloc]init];
Set your dateFormat as
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZ"];
And Yes, Now NSLog is Printing a specific Date.!!!
Enjoy Coding
I need to change string that is in 12 hour time format into NSDate. I am using below given code but its returning null. Can anyone help me with this??
NSString = #"04/03/2013 7pm";
NSDateFormatter *df = [[NSDateFormatter alloc]init];
[df setDateFormat:#"dd/MM/yyyy hh:mm a"];
NSDate *date = [df dateFromString:selectedDateString];
It is giving you null as your DateFormat does not match to your string.
Try this,
NSString *selectedDateString= #"04/03/2013 7pm";
NSDateFormatter *df = [[NSDateFormatter alloc]init];
[df setDateFormat:#"dd/MM/yyyy ha"];
NSDate *date = [df dateFromString:selectedDateString];
I want compare two dates. I am getting device date in yyyy-dd-mm format and getting mm-dd-yyyy from webservices. I want to change device date format and get that changed date in NSDate object. Here is my code:
NSDate *today = [[NSDate alloc] init];
NSLog(#"today : %#", today);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM dd yy"];
NSString *tString = [dateFormatter stringFromDate:today];
NSLog(#"tString : %#",tString);
NSDate *date = [dateFormatter dateFromString:tString];
NSLog(#"date : %#",date);
You are saying date coming from webservice is in mm-dd-yyyy format..Then convert it into NSDate like this
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM-dd-yyyy"];
NSDate *convertedDate = [dateFormatter dateFromString:yourDateStringFromWeb];
You have device date already with you..Now you can compare two NSDate objects in many ways..See this and this..