I am taking input from user as string and want to store it in date format but after printing back to console it give me NULL...
Here is what I did:
MyLog(#"\nPlease Enter Date");
scanf("%s",cDate);
stringDate= [NSString stringWithCString:cDate encoding:1];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd-MM-yyyy"];
NSDate * incomeDate = [dateFormat dateFromString:sDate];
MyLog(#"Date Entered is %#",[dateFormat stringFromDate:incomeDate]);
Make sure you are entering date string in dd-MM-yyyy as i can see you are using [dateFormat setDateFormat:#"dd-MM-yyyy"];
You can use following code to convert from string to date, but make sure you use same date format which you are taking input from user
NSString *stringtoconvert = #"13-09-2014";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd-MM-yyyy"];
NSDate *date = [dateFormat dateFromString:stringtoconvert];
MyLog(#"Date Entered is %#",date);
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 using the following code to change yyyy-MM-dd to M-dd-yyyy using NSDateFormatter but NSLog is returning the value nil. Please help. Thanks in advance.
NSString *trimmedDOB=#"1992-11-15";
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:#"MM-dd-yyyy"];
NSDate *DateOfBirth=[format dateFromString:trimmedDOB];
NSLog(#"DATE IS %#",DateOfBirth);
NSString *trimmedDOB=#"1992-11-15";
NSDateFormatter *format = [[NSDateFormatter alloc] init];
//Set your input format
[format setDateFormat:#"yyyy-MM-dd"];
//Parse date from input format
NSDate *dateOfBirth = [format dateFromString:trimmedDOB];
//Set your output format
[format setDateFormat:#"MM-dd-yyyy"];
//Output date in output format
NSLog(#"DATE IS %#",[format stringFromDate:dateOfBirth]);
Change your code like below:
NSString *trimmedDOB=#"1992-11-15";
NSDateFormatter *inFormat = [[NSDateFormatter alloc] init];
[inFormat setDateFormat:#"yyyy-MM-dd"];
NSDate *DateOfBirth=[inFormat dateFromString:trimmedDOB];
NSDateFormatter *outFormat = [[NSDateFormatter alloc] init];
[outFormat setDateFormat:#"MM-dd-yyyy"];
NSLog(#"DATE IS %#", [outFormat stringFromDate:DateOfBirth]);
NSString *trimmedDOB=#"1992-11-15";
NSDateFormatter *dateformatterIn = [[NSDateFormatter alloc] init];
[dateformatterIn setDateFormat:#"yyyy-MM-dd"];
NSDate *DateOfBirth=[dateformatterIn dateFromString:trimmedDOB];
NSDateFormatter *dateformatterOut = [[NSDateFormatter alloc] init];
[dateformatterOut setDateFormat:#"MM-dd-yyyy"];
Each date format corresponds to a single date format.
Since you have 2 date formats (one date string you are converting from and the one you are converting to) you need two date formatters.
So to convert from one date string to the other :
Create 'from' date formatter (format: yyyy-MM-dd)
Convert your source 'date string' into an 'NSDate object'
Create 'to' date formatter (format: MM-dd-yyyy)
Convert the 'NSDate object' you created in step 2 into the final 'date string'
I want to get NSDate from a string. The input strings are in format of : "2014-06-27 15:21:02"
I use the code below but i get nil:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-DD hh:mm:ss"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:edditedString];
When you look into the Data Formatting Guide you can see that your format string should be:
yyyy-MM-dd HH:mm:ss
To be able to resolve the date from your string..
Just to simplify your code, the initializer on dateFromString is not necessary, it is easier doing:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *dateFromString = [dateFormatter dateFromString:edditedString];
Sample of this in swift:
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..
I have made a calendar application for the iPhone in which I have a date in string format (e.g. "Tue, 25 May 2010 12:53:58 +0000").
I want to convert this to an NSDate.
What do I need to use to do that?
Take a look at the class reference for NSDateFormatter. You use it like this:
NSString *dateStr = #"Tue, 25 May 2010 12:53:58 +0000";
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"EE, d LLLL yyyy HH:mm:ss Z"];
NSDate *date = [dateFormat dateFromString:dateStr];
[dateFormat release];
For more information on how to customize that NSDateFormatter, try this reference guide.
EDIT:
Just so you know, this is going to parse the full month name. If you want three letter month names, use LLL instead of LLLL.
-(NSString *)dateToFormatedDate:(NSString *)dateStr {
NSString *finalDate = #"2014-10-15";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSDate *date = [dateFormatter dateFromString:dateStr];
[dateFormatter setDateFormat:#"EE, d MMM, YYYY"];
return [dateFormatter stringFromDate:date];
}
If you are storing dates in one of iOS' styles, it's far easier and less error prone to use this method:
// Define a date formatter for storage, full style for more flexibility
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterFullStyle];
[dateFormatter setTimeStyle:NSDateFormatterFullStyle];
// Use today as an example
NSDate *today = [NSDate date];
// Format to a string using predefined styles
NSString *dateString = [dateFormatter stringFromDate:today];
// Format back to a date using the same styles
NSDate *todayFromString = [dateFormatter dateFromString:dateString];
NSString *dateString=#"2017-05-25";
NSDateFormatter *dateFormatter=[NSDateFormatter alloc]init];
[dateFormatter setDateFormatter:#"MM-dd-yyyy"];
NSDate *date =[[NSDate alloc]init];
date=[datFormatter dateFromString:dateString];