NSDateFormatter issue when date string convert into NSDate object - ios

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE,d MMM YYYY"];
[dateFormatter setLocale:[NSLocale currentLocale]];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:#"Sun,2 Nov 2014"];
output date from string giving wrong output like 2013-12-21 18:30:00 +0000

Let me guess, you are doing this to test the output:
NSLog(#"%#", dateFromString);
instead do:
NSLog(#"%#", [dateFormatter stringFromDate:dateFromString]);
This is because NSLog() will call [NSDate description] to format the date and that won't account for any formatting or time zone you may want.

Related

NSDateFormatter does not formate date correctly ios10

I am trying to convert date with specific formate and just wanted to get date from passed date as shown below but getting nil
What am I doing wrong?
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss.SSS"];
NSDate *date = [dateFormatter dateFromString: [dicValues valueForKey:#"DateNeeded"]];
[dateFormatter setDateFormat:#"dd/MM/yyyy h:mm a"];
NSString *strSubTitle = [[NSString alloc] initWithFormat:#"%#", [dateFormatter stringFromDate:date]];
Date i am receiving is : 2016-11-17T23:46:50.35 , 2016-11-28T21:25:20.927 and 2016-11-29T00:00:00
You need to convert
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
to this
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS"];
As in your date string you are receiving MilliSeconds also
Edit
This code is working for me, check the date format which you are getting from server
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS"];
NSDate *date = [dateFormatter dateFromString: #"2016-11-28T21:25:20.927"];
if (date == nil) {
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];
date = [dateFormatter dateFromString: #"2016-11-28T21:25:20.927"];
}
[dateFormatter setDateFormat:#"dd/MM/yyyy h:mm a"];
NSString *strSubTitle = [[NSString alloc] initWithFormat:#"%#", [dateFormatter stringFromDate:date]];
[dicValues valueForKey: #"DateNeeded"] is either nil or does not match the specified format "yyyy-MM-dd HH:mm:ss".
First check that date you receive, is in yyyy-MM-dd HH:mm:ss format. If not then make this format according to date you received.
For Example, you received date 29/11/16, then your code will be like this
[dateFormatter setDateFormat:#"dd/MM/yy"];
NSDate *date = [dateFormatter dateFromString: [dicValues valueForKey:#"DateNeeded"]];
Once you get date, then convert it any format you want.
try
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS"];

iOs - error with NSDateFormatter

I have a Date with value 2015-05-31 22:00:00 UTC (I parse it from a NSString with value "01-06-2015" and it give that value) and when I parse it it become 01-06 why??
I need to get on nextdate the value = 2015-06-1 22:00:00 UTC because I need to get the day of the week
Code:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"es_ES"];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
[dateFormatter setLocale:enUSPOSIXLocale];
NSDate *nextDate = [theCalendar dateByAddingComponents:comps2 toDate:date2 options:0];
NSString* primeraFecha = [dateFormatter stringFromDate:nextDate];
Values at runtime
Try with this code,
NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
[dateFormatter setTimeZone:[[NSTimeZone alloc] initWithName:#"Europe/London"]];
return [dateFormatter dateFromString:dateString];
You should set the time zone of your formatter:
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

UIDatePickerModeDateAndTime format 2012-12-20 07:15:18 +0000 without +0000

UIDatePickerModeDateAndTime returns an output in format
2012-12-20 07:15:18 +0000.
How can I remove the +0000 from the output?
I wanted to make a screenshot, but yet my reputation is not enough, I hope it will clear my question.
use a NSDateFormatter
- (IBAction)dateChanged:(UIDatePicker *)sender {
NSDate *date = sender.date;
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateStyle:NSDateFormatterShortStyle]; // if you want to show the date to the user
[df setTimeStyle:NSDateFormatterShortStyle]; // if you want to show the date to the user
// [df setDateFormat:#"yyyy-MM-dd HH:mm:ss"]; // if you want to use the date for your model
NSString *dateString = [df stringFromDate:date];
NSLog(#"%#", dateString);
}
If you want to show the date to the user please use setDateStyle: and setTimeStyle:, because they are locale aware and produce the output in the correct format.
If you need that date only for the back end (e.g. creating a file name) use setDateFormat:.
There are many solutions for this. Some of these are:
1) substitution of + 0000 by empty string
2) slicing the output until the (length - 5)
It requires creativity to develop more solutions.
Use this way:
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyy-MM-dd HH:mm:ss"];
NSDate *today = [NSDate date];
NSString *dateString =[dateFormatter stringFromDate:today];
NSLog(#"Date is: %#",dateString);
Output:
Date is: 2012-12-20 12:58:58
Here is code
NSString *calDate = #"2011-11-11 21:56:38 +0000";
NSDate *date = [[[NSDate alloc] init] retain];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];
// right here ^
date = [dateFormatter dateFromString:calDate];
NSLog(#"date:%#", date);
Outputs:
2012-01-03 20:14:15.258 Craplet[38753:707] date:2011-11-11 21:56:38 +0000
Without the Z:
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
It outputs:
2012-01-03 20:16:10.456 Craplet[38885:707] date:(null)
If the format cannot be matched to the date string, it returns nil

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

Conversion of NSString to NSDate failing

I have the following piece of code..
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM/dd/YYYY HH:mm"];
NSString *dateString = #"10/27/2012 18:00";
NSDate *parsedDate = [dateFormatter dateFromString:dateString] ;
NSLog(#"Parsed Date.. %#",[parsedDate description]);
I am getting the NSLog statement as Parsed Date.. 2011-12-25 00:00:00 +0000. I would appreciate any help in resolving this. I just want to convert that NSString to its NSDate equivalent. Tried using NSTimeZone as well, but no effect. What am I missing here?
You need to use lower case year indication, yyyy instead of YYYY:
[dateFormatter setDateFormat:#"MM/dd/yyyy HH:mm"];
If you want to get the timezone right you need to set the timezone of the NSDateFormatter. By default it uses the timezone of your device. For the time at GMT use the following.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
[dateFormatter setDateFormat:#"MM/dd/yyyy HH:mm"];
NSString *dateString = #"10/27/2012 18:00";
NSDate *parsedDate = [dateFormatter dateFromString:dateString] ;
NSLog(#"Parsed Date.. %#",[parsedDate description]);
You are using an incorrect format string. See the documentation. Basically, capital YYYY is "week of year year". Try to use lowercase yyyy instead, i.e.
[dateFormatter setDateFormat:#"MM/dd/yyyy HH:mm"];

Resources