My string is strDate:04/12/2016 on converting it to date in format dd/MM/yyyy. It is producing another format.it is producing result as weekEndDate:2016-12-04 00:00:00 +0000. My code is
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd/MM/yyyy"];
weekEndDate=[formatter dateFromString:strDate];
2016-12-04 00:00:00 +0000
This is not a wrong format, whenever you will print an object of NSDate in your debugger that object will be printed like the above string i.e. in UTC timezone, if you want to check correct format then convert this date object in string and print that string you will get correct format.
It's normal that you get that format, if you want to get your weekend date using that formatter here is how you do it :
NSString *strDate = #"04/12/2016";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd/MM/yyyy"];
NSDate *weekEndDate = [formatter dateFromString:strDate];
NSLog(#"%#", [formatter stringFromDate:weekEndDate]); // 04/12/2016
NSLog(#"%#", weekEndDate); // 2016-12-04 00:00:00 +0000
Related
So, I have the following Date string that commes from a Server:
2020-05-09 18:33:00 CET
So, in order to get a NSDate from that NSString I use the following code:
+(NSDate*) converFirebaseStringDateToDate:(NSString*)dateStr {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss zzz"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateStr];
return dateFromString;
}
But this doesn't seem to be working, always the return dateFromString is NIL (as if the date format was invalid) which is not according to the documentation.. What am I missing here? This very same peace of code is working just fine with string like: 2020-05-09 16:26:33 GMT or even 2020-05-09 12:35:02 EDT
Also been trying to use 2020-05-09 18:33:00 CEST as Input but still failing!
I know this question has been asked so many time and may be duplicate of some question, actually i am trying this for storing Date into array by converting them in String. I need that Value in NSDate format so i again convert that stored string into Date.
NSDateFormatter *dateformat = [[NSDateFormatter alloc] init];
[dateformat setDateFormat:#"MM-dd-yy-hh-mm-ss"];
NSString *date = [dateformat stringFromDate:datePicker.date];
[kAppDelegate.glbArrName addObject:date];
But I get this output :
NSString *date = [kAppDelegate.glbArrDate objectAtIndex:indexPath.row];
NSLog(#"Date of birth %#",date);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"MM-dd-yy-hh-mm-ss"];
NSDate *birthDate = [[NSDate alloc] init];
birthDate = [dateFormatter dateFromString:date];
NSLog(#"Date of birth after formatting %#",birthDate);
Output is:
Date of birth 04-05-16-06-38-14
Date of birth after formatting 2016-04-05 01:08:14 +0000
Why it changes format, as i have done same as previous. please help me find out ..
You're rewriting the date string back into an NSDate, then printing out the NSDate, which defaults to the latter 2016-04-05 01:08:14 +0000 format as part of NSDate's -description.
I googled so many times, but I am not satisfied with given answer. Please anyone can give correct answer what I need.
This is the retrieve date string from DB : 2015-05-27 10:19 (yyyy-MM-dd HH:mm)
I want to convert into NSDate.
My code is like Below:
NSString *date_str = #"2015-05-27 10:19";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm"];
NSDate *date = [dateFormat dateFromString:date_str];
NSLog(#"date == %#",date);
But output is : date == 2015-05-27 04:49:00 +0000
Its showing 04:49:00 time , but my retrieve time is 10:19.
How can i retrieve perfect time from DB.
Please help out me..
Just convert your date to GMT time like this:-
NSString *date_str = #"2015-05-27 10:19";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormat setTimeZone:gmt];
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm"];
NSDate *date = [dateFormat dateFromString:date_str];
And the output is :-
date == 2015-05-27 10:19:00 +0000
And to get the date without +0000, you can store it in NSString directly:-
NSString *s = [dateFormat stringFromDate:date];
Output :-
2015-05-27 10:19
You are facing the timezone issue with conversion, because server time and local timezone may have difference. So handle this you need to set the timezone to your dateformatter like
[dateFormater setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
I have a date-time string returned by the server in UTC format: 2015-04-21T00:54:46.469Z
I am trying to convert this into NSDate. The code is:
NSString *dateString = #"2015-04-21T00:54:46.469Z";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-mm-DDThh:mm:ss.sZ"];
NSDate *locationDate = [dateFormatter dateFromString:dateString];
The value of locationDate is nil after executing this
You should use
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
or
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
Any way, T must be in quotes. It seems to be a bug in Apple.
I have this string date:
2014-04-21T07:55:13Z
when I convert that to NSDate I have the hour like 6:55... 1 hours less. WHY?
This is the code I am using to convert:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSDate *newDate = [dateFormatter dateFromString:dateStr];
newDate is now 2014-04-21 06:55:13 +0000 !!!???
what is wrong?
NOTE: That one hour less would make sense if the date was my local time (GMT+1) being converted to GMT. But if that Z is zero offset ( = GMT) the date is already GMT.
I don't think your code is wrong. using this code:-
NSString *dateStr = #"2014-04-21T07:55:13Z";
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSDate *date = [dateFormat dateFromString:dateStr];
NSLog(#" date log %#",date); //2014-04-21 02:25:13 +0000 output
// Convert date object to desired output format
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
dateStr = [dateFormat stringFromDate:date];
NSLog(#"string %#",dateStr); //2014-04-21T07:55:13Z output
but NSLog of NSDATE is not output correct according to this NSDate Format outputting wrong date so your code is right.
The NSDate doesn't know anything about formatting (just date information), and the NSDateFormatter doesnt really know anything about dates, just how to format them. So you have to use methods like -stringFromDate: for know that is current or not to actually format the date for pretty human-readable display.
NSLog(#" date is %#",[dateFormat stringFromDate:date]);