Converting NSString to Epoch timestamp iOS7 - ios7

I need to convert the following date to epoch timestamp
Wed, 21 May 2014 16:47:00 BST
stringPubDate has the above string stored in it.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, d MMMM yyyy HH:mm:ss z"];
NSDate *date = [dateFormatter dateFromString:stringPubDate];
Null is being returned when I do this. I need to convert this NSString to NSDate and then convert it into epoch timestamp. Can someone please help me?

The problem is that the date format you are giving does not match the format of the date in the string.

Related

NSDateFormater stringFromDate to dateFromString return wrong date [duplicate]

This question already has an answer here:
NSDateFormatter show wrong year
(1 answer)
Closed 6 years ago.
My code:
NSString *dateStr = #"03-02-2017";//[responseObject objectForKey:#"event_date"];
NSLog(#"'%#'", dateStr);
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_HK"];
[dateFormat setLocale:locale]; //To fix the format into something like: 10 Feb 2017, but not: 10 2月 2017
[dateFormat setDateFormat:#"dd-MM-YYYY"];
NSDate *date = [dateFormat dateFromString:dateStr];
NSLog(#"'%#'", date);
// Convert date object to desired output format
[dateFormat setDateFormat:#"dd MMM YYYY"];
dateStr = [dateFormat stringFromDate:date];
NSLog(#"'%#'", dateStr);
And in log it returns
'03-02-2017'
'2016-12-24 16:00:00 +0000'
'25 Dec 2016'
Anyone know why this happened? I searched in google that most cases are causing by timezone. However, I'm pretty sure it's not about timeZone because the difference of dates are too large, but I can't figure out the root of this problem.
I think your dateformat is wrong.
It should be
dd-MM-yyyy
I found that my format is wrong from answer objective-c - NSDateFormatter returns wrong date
and reference by http://nsdateformatter.com .
It should be "yyyy" instead of "YYYY"

objective c convert string to UTC- the time is moving in error

I return a string from my database and value is '04/27/2016 1:16pm'. This value is already in UTC.
Now I want to convert that string to NSDATE, keeping it in UTC. When I try to convert string to date, the time is actually moving by 1 hour.
This is how I am doing it
NSString *tuploadtime = [tempDictionary valueForKey:#"uploadTime"];
//date conversions
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM-dd-yyyy HH:mm:ss a"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
NSDate *duploadtime = [[NSDate alloc] init];
duploadtime = [dateFormatter dateFromString:tuploadtime];
NSLog(#"tuploadtime=%#, duploadtime=%#", tuploadtime, duploadtime);
the result is coming back as 2016-04-27 12:16:01 UTC.
result is
2016-04-27 10:12:44.612 x[1558:48608] tuploadtime=4/27/2016 2:10:33 PM, duploadtime=2016-04-27 12:10:33 +0000
basically the time is moving back 1 hour but I want to keep it the same.
hope I am making sense
Proper String Format for Date is most Important.
There are some methods to have HOURS format as follow with their differences..
kk: will return 24 format Hour in (01-24) hours will (look like 01, 02..24).
HH will return 24 format Hour in (00-23) hours will(look like 00, 01..23).
hh will return 12 format Hour (look like 01, 02..12).
so you should use your code like
NSString *tuploadtime = [tempDictionary valueForKey:#"uploadTime"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM-dd-yyyy hh:mm:ss a"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
NSDate *duploadtime = [[NSDate alloc] init];
duploadtime = [dateFormatter dateFromString:tuploadtime];
NSLog(#"tuploadtime=%#, duploadtime=%#", tuploadtime, duploadtime);
for More Date format refer this link
Your date format string is inconsistent:
[dateFormatter setDateFormat:#"MM-dd-yyyy HH:mm:ss a"];
HH means to use a 24-hour format. But then you also used a to indicate AM/PM. Using both is confusing the formatter and giving you off-by-one. You meant to use hh here.

NSDateFormatter showing nil

NSString *releaseTime = #"2015-10-30 16:00:00";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"yyyy-MM-dd hh:mm:ss";
NSDate *releaseDate = [dateFormatter dateFromString:releaseTime];
When i try to access this releaseDate it is showing nil.
Change the format from yyyy-MM-dd hh:mm:ss to yyyy-MM-dd HH:mm:ss and it should work.
If you wonder why, hh is for am/pm hour format, while HH is for 24 hour format. Since you have 16:00:00 it's 24h format and the date formatter can't parse it and returns nil.

String to Date and Time formate and date and time to string in objectiveC

I tried to convert String to date time and date time to string it's working but some what error. I not getting correct formate.
my excepting result should be:
like this.02/02/2013 05:24PM
time should be 12 hours formate;
NSString *dateTime = #"2013-02-02 15:54:31";
int time=[[temp objectForKey:#"created"]intValue];
NSLog(#"Time=%d",time);
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"YYYY-MM-DD HH:MM:SS"];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:dateTime]];
NSDate *date = [dateFormat dateFromString:dateTime];
NSLog(#"Nsdate=%#",date);
// Convert date object to desired output format
[dateFormat setDateFormat:#"hh:mma"];
dateTime = [dateFormat stringFromDate:date];
[dateFormat release];
NSLog(#"DateTime=%#",dateTime);
--------------------
NSLogs :
Time=2013
DateTime=2013-02-02 17:06:09
Nsdate=2013-01-02 11:30:00 +0000//it's not correct date
DateTime=02/01/2013 05:00PM//it's not correct date and time//
-------------
my excepting result should be like this:
DateTime :02/02/2013 05:24PM
If you have any idea please shear your ideas , It's will help me.
Change your this line
[dateFormat setDateFormat:#"YYYY-MM-DD HH:MM:SS"];
to
[dateFormat setDateFormat:#"YYYY-MM-dd HH:mm:SS"];
mm and dd should be small
Hope it helps you..

Issues with String to NSDate

My dateFromString is not working and i'm not sure why
NSString *purchase = #"2011-09-30 17:47:57";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
NSDate *date = [dateFormat dateFromString:purchase];
date is 'invalid CFStringRef'
See anything i might be overlooking?
Try changing the hours in the formatter to capitals, i.e. yyyy-MM-dd HH:mm:ss
I'm not sure if that will solve your error, but that is the correct way to parse a 24 hour time
in the format
"hh" means Hour [1-12].
"HH" means Hour [0-23].
See UTS Date Field Symbol Table for the date format specifiers.
Try this:
NSString *purchase = #"2011-09-30 17:47:57";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [dateFormat dateFromString:purchase];
Date Formatters Guide - HH is used for 24 hour times, hh for 12 hour.
If you mean that "date" is not accepted as an NSString by another function, that's because it's not a string, it's an NSDate.
(Where, precisely, are you getting the error message, and what is the full text?)