NSDate comparison in custom format Issue - ios

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..

Related

Unable to convert NSString to NSdate format

I want to convert my input string to particular NSDate format,
Here is the code I have tried.
NSString * result;
NSDate * resultDate;
NSDate *date = [dateFormat dateFromString:maxUpcomingDateString];
NSDateFormatter *dateFormat1 = [[NSDateFormatter alloc] init];
dateFormat1.locale=[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[dateFormat1 setDateFormat:#"yyyy/MMM/dd"];
NSLog(#"maxUpcomingDateString %#",maxUpcomingDateString)
NSDate *d=[dateFormat1 dateFromString:maxUpcomingDateString];
result = [dateFormat1 stringFromDate:date];
resultDate=[dateFormat1 dateFromString:result];
NSLog(#"max1 date upcoming %#",result);
NSLog(#"max2 date upcoming %#", resultDate);
Output of my log shows:
maxUpcomingDateString 10 January 2017
max1 date upcoming 2017/Jan/10
max2 date upcoming (null)
I want the output date in the form of 2017/Jan/10. When I try to log the string it shows me the correct output but when I convert it back to NSDate it shows null.
nsstring to nsdate
NSString *dateString = #"01-02-2010";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// this is imporant - we set our input date format to match our input string
// if format doesn't match you'll get nil from your string, so be careful
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
nsdate to nsstring
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSString *stringDate = [dateFormatter stringFromDate:[NSDate date]];
NSLog(#"%#", stringDate);
add this code on dateFormat
[dateFormat setDateFormat:#"dd MMM yyyy"];
It will solve the problem.
Add this code after allocating the dateFormat object.
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd MMM yyyy"];

iOS NSDateFormatter returns old date from string

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

How to get NSDate in "dd/MM/yyyy" formate ios?

I am working with NSDate i created one function which give Today date with "dd/MM/yyyy" format but NSDate always show "nil" value. I googled lot and also see lots of stackoverflow answer but all answer return NSString value not NSDate value so please guide me to get NSDate with given format.
+(NSDate*)getCurrentDate{
NSDateFormatter *DateFormatter=[[NSDateFormatter alloc] init];
[DateFormatter setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
//Date print here
NSLog(#"Utility CurrentDate: %#",[DateFormatter stringFromDate:[NSDate date]]);
NSString *dateString = [DateFormatter stringFromDate:[NSDate date]];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSDateFormatter *newDateFormatter = [[NSDateFormatter alloc]init];
[newDateFormatter setDateFormat:DATE_FORMAT];
//Date print here
NSLog(#"New Date: %#",[newDateFormatter stringFromDate:[NSDate date]]);
NSDate *dateFromString = [newDateFormatter dateFromString:dateString];
return dateFromString;
}
The issue is here
NSDate *dateFromString = [newDateFormatter dateFromString:dateString];
Here your dateString is 2014-12-11 03:41:31, hence it is not getting formatted. You should change either newDateFormatter or DateFormatter to convert as per your requirement.
EDIT:
Replace [DateFormatter setDateFormat:#"yyyy-MM-dd hh:mm:ss"]; with [DateFormatter setDateFormat: DATE_FORMAT];
But your return date will not be in the desired way, since you are returning an NSDate Object. You should return a formatted string.
You are completely misunderstanding what NSDate is and does. An NSDate is an absolute point in time. It has no format. It cannot have a format. It is absolutely impossible to have an NSDate in "dd/MM/yyyy" format or in any other format.
When you want to display a date to the user, you use NSDateFormatter to convert the NSDate into a string. In any format you like.
check here Convert date from string or you can use this methods to convert.
These all three functions you can use to format date values..
-(NSString*)getStringFromDate:(NSDate*)pDate withFormat:(NSString*)pDateFormat{
NSDateFormatter *dtFormatter = [[NSDateFormatter alloc] init];
[dtFormatter setDateFormat:pDateFormat];
[dtFormatter setLocale:[NSLocale systemLocale]];
return [dtFormatter stringFromDate:pDate];}
-(NSDate*)getDateFromString:(NSString*)pStrDate withFormat:(NSString*)pDateFormat{
NSDateFormatter *dtFormatter = [[NSDateFormatter alloc] init];
[dtFormatter setDateFormat:pDateFormat];
[dtFormatter setLocale:[NSLocale systemLocale]];
return [dtFormatter dateFromString:pStrDate];}
-(NSString*)dateStringFromString:(NSString *)dateToConver format:(NSString *)fromFormat toFormat:(NSString *)toFormat{
NSDate *date = [Helper getDateFromString:dateToConver withFormat:fromFormat];
return [Helper getStringFromDate:date withFormat:toFormat];}
Refer the below modified method in dd/MM/yyyy format:-
+(NSString*)getCurrentDate{
NSDateFormatter *format=[[NSDateFormatter alloc]init];
[format setDateFormat:#"yyyy-MM-dd HH:mm:ss ZZZ"];
NSString *currentDt=[format stringFromDate:[NSDate date]];
NSDate *dt=[format dateFromString:currentDt];
[format setDateFormat:#"dd/MM/yyy"];
return [format stringFromDate:dt];
}

Getting null when trying to extract year value from a date [duplicate]

This question already has answers here:
NSDate from NSString gives null result
(3 answers)
Closed 9 years ago.
My code is the following:
NSString *dateString = #"1339007317";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy"];
NSDate *date = [dateFormatter dateFromString:dateString];
NSString *year = [dateFormatter stringFromDate:date];
NSLog(#"%#",year);//null
How to fix that? thanx.
It is because you are using unix timestamp and you can convert unix timestamp into year or any date format as :
//Posted Date Format
NSString *dateStr = #"1339007317";
NSDate *date = [NSDate dateWithTimeIntervalSince1970:[dateStr doubleValue]];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en-US"];
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateFormat:#"yyyy"]; //Here you can set any date format Ex:#"dd MMM, yyyy hh:mm a" or#"dd/MM/yyyyy" according to your requirement
[dateFormatter1 setLocale:locale];
[dateFormatter1 setTimeZone:[NSTimeZone systemTimeZone]];
dateStr = [dateFormatter1 stringFromDate: date];
Hope it helps you.
You need two date formats. The first should match the format of the string you wish to convert to an NSDate. The second format needs to represent the format you want. Use the 2nd to convert the NSDate to the new string.
Also, why do you create a date object then immediately replace it with a new one?
Change this:
NSDate *date = [[NSDate alloc] init];
date = [dateFormatter dateFromString:dateString];
to:
NSDate *date = [dateFormatter dateFromString:dateString];
Your code needs to be like this:
NSString *dateString = #"1339007317";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"???"]; // format that matches the dateString
NSDate *date = [dateFormatter dateFromString:dateString];
[dateFormatter setDateFormat:#"yyyy"]; // the desired new format
NSString *year = [dateFormatter stringFromDate:date];
NSLog(#"%#",year);

Troubles with parsing ISO8601 time in iOS

I'm trying to convert this string "2011-11-23T17:59:00Z" to an NSDate.
I've seen many people have this problem, but everyone has a slightly different format. I haven't been able to hack a solution.
I've tried code such as:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
dateFormat.timeStyle = NSDateFormatterFullStyle;
[dateFormat setDateFormat: #"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSString* date = ""2011-11-23T17:59:00Z"";
NSString *dateString = [dateFormat stringFromDate: date];
dateString always comes back NULL
NSDateFormatter stringFromDate takes an NSDate object and you're passing it a string.
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html
Try NSDateFormatter dateFromString.
If you use dateFromString:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat: #"yyyy-MM-dd'T'HH:mm:ssZ"];
NSString* dateStr = #"2011-11-23T17:59:00Z";
NSDate *date = [dateFormat dateFromString:dateStr];
NSLog(#"date: %#", date);
Outputs:
2011-12-14 00:34:57.587 Craplet[440:707] date: 2011-11-23 22:59:00 +0000

Resources