NSDateFormatter in iOS - ios

I'm using the Twitter API to download the number of tweets because I need them in my app for iOS. I'm using this function to do this. The problem is that I need to know the day, month and year when the tweet was written.
The date that comes back is like this:
Thu Oct 25 10:34:58 +0000 2012
But I need a date like this format:
25/10/2012 10:34
So, I want to transform the first format to the second format. I've tried to do it with NSDate and NSDateFormatter, obtaining a NSDate with the first NSString, and after transforming this NSDate in the second NSString, but I didn't get it.
NSArray *dateArray = [tweets valueForKey:#"created_at"];
NSString *dateString = [dateArray objectAtIndex:0]; // Thu Oct 25 10:34:58 +0000 2012
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
NSDate *date = [dateFormatter dateFromString:dateString];
NSLog(#"%#", date);
[dateFormatter setDateFormat:#"dd/MM/yyyy"];
NSString *realDateStr = [dateFormatter stringFromDate:date];
NSLog(#"%#", realDateStr);
The two NSLogs each return null.
Can anybody help me? Thanks.

Try setting the date format originally to parse out the date string you are getting, like this:
NSString *dateString = #"Thu Oct 25 10:34:58 +0000 2012";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE MMM dd hh:mm:ss Z yyyy"];
NSDate *date = [dateFormatter dateFromString:dateString];
NSLog(#"%#", date);
[dateFormatter setDateFormat:#"dd/MM/yyyy hh:mm"];
NSString *realDateStr = [dateFormatter stringFromDate:date];
NSLog(#"%#", realDateStr);

Try this simple method:
NSString* string=#"26-01-2014";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-mm-yyyy"];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter setTimeZone:gmt];
NSDate *date = [dateFormatter dateFromString:string];
NSLog(#"Date = %#",date);

Related

Thirteen figures converted into NSDate

Code i have try
NSString *time = #"1470067200000";
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:#"YYYY-MM-dd HH:mm"];
NSDate *date = [NSDate dateWithTimeIntervalSince1970:(NSTimeInterval)([time doubleValue])/1000];
NSString *dateString = [formatter stringFromDate:date];
NSLog(#"%#",dateString);
Result : 2016-08-02 00:00
The HH:mm is always wrong, why?
Set Time Zone in the NSDateFormatter
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm"];
[formatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
NSDate *date = [NSDate dateWithTimeIntervalSince1970:(NSTimeInterval)([time doubleValue])/1000];
NSString *dateString = [formatter stringFromDate:date];
NSLog(#"%#",dateString);
NSDate always be in UTC and if you apply DateFormatter then default timezone in DateFormatter is localTimeZone. When I run the below code its giving me the below dates. Please check your device/simulator timezone
NSString *time = #"1470067200000";
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:#"YYYY-MM-dd HH:mm"];
// [formatter setTimeZone:[NSTimeZone localTimeZone]];
NSDate *date = [NSDate dateWithTimeIntervalSince1970:(NSTimeInterval)([time doubleValue])/1000];
NSString *dateString = [formatter stringFromDate:date];
NSLog(#"%#",dateString);
Printing description of date:
2016-08-01 16:00:00 +0000
Printing description of dateString: // after From NSDateFormatter
2016-08-01 21:30
To get the stringFromDate in UTC from NSDateFormatter use below line
[formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];

How to get the UTC time from NSDate iOS

I want to log the server time in my iOS app , I am using NHNetworkTime library to get the network time, when I try to print the following code its showing GMT, through I have chosen UTC, how to print the date time in UTC?
NSDate* datetime = [NSDate networkDate];
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]]; // Prevent adjustment to user's local time zone.
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss.SSS Z z "];
NSString* dateTimeInIsoFormatForZuluTimeZone = [dateFormatter stringFromDate:datetime];
NSLog(#"%#",dateTimeInIsoFormatForZuluTimeZone);
this is printing 2016-03-09 06:51:23.406 +0000 GMT
There is no time difference between Coordinated Universal Time (UTC) and Greenwich Mean Time(GMT). you can get more info from here More info
Refer :-
-(NSString *)getUTCFormateDate:(NSDate *)localDate
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:#"UTC"];
[dateFormatter setTimeZone:timeZone];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSString *dateString = [dateFormatter stringFromDate:localDate];
[dateFormatter release];
return dateString;
}
Moreover to get time
-(NSString *)getTimeFromDate:(NSDate *)localDate
{
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc]init]autorelease];
dateFormatter.dateFormat = #"MM/dd/yy";
NSString *dateString = [dateFormatter stringFromDate: localDate];
NSDateFormatter *timeFormatter = [[[NSDateFormatter alloc]init]autorelease];
timeFormatter.dateFormat = #"HH:mm:ss";
return [timeFormatter stringFromDate: localDate];
}

Why converting date format is wrong in XCode

- (NSString *) displayInvitationDate:(NSString *)paramDate {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-mm-dd"];
NSDate *date = [dateFormatter dateFromString: paramDate];
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE dd MMM"];
NSString *convertedString = [dateFormatter stringFromDate:date];
return convertedString;
}
That's calling above function
[self displayInvitationDate:#"2016-02-27"];
Oddly result is
Wed 27 JAN
and result should be
SAT 27 JAN
Please let me know which part got wrong above my coding.
You should add this line.
[dateFormatter setTimeZone:[NSTimeZonetimeZoneWithAbbreviation:#"GMT"]];
and chanfe your pass foemat yyyy-mm-dd to replace yyyy-MM-dd, For set as....
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
For more Details Data time Format to Click Now

NSDateFormatter for day and month name

I am trying to get date in the format Monday, March 09, 2015. But following code is not returning required date format. I think I am using wrong Formatter. Here is the code:
NSString *dateString = #"09-03-2015";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd MMMM yyyy"];
NSDate *date = [[NSDate alloc] init];
date = [dateFormatter dateFromString:dateString];
NSLog(#"%#",[dateFormatter stringFromDate:date]);
try this...
//Getting date from string
NSString *dateString = #"09-03-2015";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSDate *date = [[NSDate alloc] init];
date = [dateFormatter dateFromString:dateString];
// converting into our required date format
[dateFormatter setDateFormat:#"EEEE, MMMM dd, yyyy"];
NSString *reqDateString = [dateFormatter stringFromDate:date];
NSLog(#"date is %#", reqDateString);
LOG:2015-03-09 12:40:33.456 TestCode [1377:38775] date is Monday, March 09, 2015
Firstly you must convert your dateString to NSDatevalue
NSString *dateString = #"09-03-2015";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSDate *date = [[NSDate alloc] init];
date = [dateFormatter dateFromString:dateString];
NSLog(#"%#",[dateFormatter stringFromDate:date]);
after that you could use this format: #"EEEE,MMMM dd, yyyy" to convert that dateValue to dateString like Monday, March 09, 2015
Hope this help
Helpful link for you
Try to set dateFormat property to #"dd'-'MM'-'yyyy" . Always check the unicode the date symbol table. "MMMM" means that the date month should be a full month name.
Try this one..
NSString * yourJSONString = #"09-03-2015";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];;
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
[dateFormatter setLocale:[NSLocale currentLocale]];
NSDate *dateFromString = [dateFormatter dateFromString:yourJSONString];
[dateFormatter setDateFormat:#"EEEE,LLLL dd, yyyy"];
NSString *output = [dateFormatter stringFromDate:dateFromString];
NSLog(#"%#", output);
Try this:-
NSDate *date = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSString * format = [NSDateFormatter dateFormatFromTemplate:#"EEEEMMMMdyyyy" options:0 locale:[NSLocale currentLocale]];
[formatter setDateFormat:format];
NSString *dateFormatted = [fullDateFormatterTime stringFromDate: date];
NSLog(#"Formatted date: %#", dateFormatted);

how to convert nsdate to another format using nsdateformatter

I have date string in this format Feb 23, 2014 coming from web services, I want to convert it to this format 02-23-2014, I am trying a lot but all the time nsstring return me null value
NSDateFormatter *DateFormatter=[[NSDateFormatter alloc] init];
[DateFormatter setDateStyle:NSDateFormatterMediumStyle];
[DateFormatter setDateFormat:#"MM-dd-yyyy"];
// date formatter two
NSDate *dateVal = (NSDate *)bookDetail.classdate;
NSLog(#"date %#",dateVal);
NSString *dateTwo = [DateFormatter stringFromDate:dateVal];
NSLog(#"new date string %#",dateTwo);
NSString *dateString = #"Feb 23, 2014";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMM dd, yyyy"];
NSDate *date = [dateFormatter dateFromString:dateString];
[dateFormatter setDateFormat:#"MM-dd-yyyy"];
NSString *newDateString = [dateFormatter stringFromDate:date];

Resources