Can not format pubDate to NSDate iOS - ios

I can not figure out why NSDate continues to throw nil.
NSString * copyString = [[self.parseResults objectAtIndex:indexPath.row]objectForKey:#"date"];
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
[df setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss zzz"];
NSDate *date = [df dateFromString:copyString];
NSLog(#"%#", copyString);
NSLog(#"%#",date);
Did I set the date format set properly?
Output from copyString
2014-01-24 11:17:25.893 Events[32755:70b] Wed, 31 Dec 1969 16:00:00
PST
2014-01-24 11:17:25.895 Events[32755:70b] Fri, 24 Jan 2014 20:00:00
PST
2014-01-24 11:17:25.896 Events[32755:70b] Sat, 25 Jan 2014 10:00:00
PST
2014-01-24 11:17:25.897 Events[32755:70b] Mon, 27 Jan 2014 10:00:00
PST
2014-01-24 11:17:25.899 Events[32755:70b] Mon, 27 Jan 2014 12:15:00
PST
2014-01-24 11:17:25.900 Events[32755:70b] Mon, 27 Jan 2014 19:00:00
PST
Output from date
2014-01-24 11:22:24.707 Events[32827:70b] (null)
2014-01-24 11:22:24.709 Events[32827:70b] (null)
2014-01-24 11:22:24.710 Events[32827:70b] (null)
2014-01-24 11:22:24.712 Events[32827:70b] (null)
2014-01-24 11:22:24.713 Events[32827:70b] (null)
2014-01-24 11:22:24.714 Events[32827:70b] (null)

Related

In iOS Swift How can I convert the time from the format "Tue Aug 27 2019 07:01:59 GMT+0530 (India Standard Time)" to YYYY-MM-DD

From backend i am getting date as "Tue Aug 27 2019 07:01:59 GMT+0530 (India Standard Time)".
In iOS Swift, How can I convert the time from the format "Tue Aug 27 2019 07:01:59 GMT+0530 (India Standard Time)" to YYYY-MM-DD.
Removed 'GMT+0530 (India Standard Time)' from date string what are you getting from server
then use this -
let date = self.convertDateStringToFormatString1(dateString:
"Tue Aug 27 2019 07:01:59", fromFormat: "E MMM dd yyyy hh:mm:ss" , toFormat:
"YYYY-MM-dd")
func convertDateStringToFormatString1(dateString:String, fromFormat : String,
toFormat : String) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = fromFormat
let date = dateFormatter.date(from: dateString)
dateFormatter.dateFormat = toFormat
dateFormatter.locale = NSLocale.current
return dateFormatter.string(from: date!)
}

dateFromString returns nil for some values

I am getting nil for some values while using dateFromString in swift. I searched a lot but in vain.
Following is my code:
let strDate = self.sortedDict.valueForKey("TIME").objectAtIndex(indexPath.row).objectAtIndex(0) as? String
print(strDate);
let st_date = frmt.dateFromString(strDate!)
let frmt1:NSDateFormatter = NSDateFormatter()
frmt1.locale = NSLocale(localeIdentifier: localeStr)
frmt1.dateFormat = "MMM, dd yyyy hh:mm a"
if st_date != nil {
print(st_date)
}
Output console:
Optional("September, 20 2015 10:00:00")
Optional(2015-09-20 10:00:00 +0000)
Optional("October, 04 2015 10:00:00")
Optional(2015-10-04 10:00:00 +0000)
Optional("October, 04 2015 14:00:00") // nil
Optional("October, 18 2015 15:00:00") // nil
Optional("September, 20 2015 14:00:00") // nil
Optional("September, 27 2015 10:00:00")
Optional(2015-09-27 10:00:00 +0000)
Optional("September, 27 2015 12:00:00")
Optional(2015-09-27 00:00:00 +0000)
Optional("September, 27 2015 14:00:00")
Optional("October, 03 2015 14:00:00") //nil
Optional("October, 03 2015 16:00:00") //nil
The format is same for all date strings still I get nil for few values. Why so? Please help. Where am I getting wrong?
format should be HH for 24 hours even you are getting values only for 12 hours.
frmt1.dateFormat = "MMM, dd yyyy HH:mm a"

IOS Components From Date Changes Timezone

I am using
NSDateComponents *components = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:_startDate];
[components setHour:0];
[components setMinute:0];
[components setSecond:0];
_date = [CURRENT_CALENDAR dateFromComponents:components];
With dates I receive from an API.
The _date returns 2 different outputs depending on the calendar day:
2016-01-04 05:00:00 +0000
2015-10-26 04:00:00 +0000
As if there was a change in time zone.
Is there a reason the time of _date changes from 5 to 4 ?Is there something to prevent that?
Problem is that unexpected time offset (-1) reflects in all the other dates I create with dateFromComponents:components
Output for different dates showing the offset
2016-01-04 05:00:00 +0000
2015-12-21 05:00:00 +0000
2015-12-14 05:00:00 +0000
2015-12-07 05:00:00 +0000
2015-11-23 05:00:00 +0000
2015-11-16 05:00:00 +0000
2015-11-09 05:00:00 +0000
2015-11-02 05:00:00 +0000
2015-10-26 04:00:00 +0000
2015-10-19 04:00:00 +0000
2015-10-22 04:00:00 +0000
2015-10-01 04:00:00 +0000
2015-09-24 04:00:00 +0000

Is the Time object suitable to create a calendar?

I want to make a database-backed calendar. Will the Time object make my life easier? It hasn't so far...
The .end_of_year method gives me some strange information. If it's contemporary time it works flawlessly:
date = '2012-3-2'.to_time(:utc) #=> 2012-03-02 00:00:00 UTC
date.end_of_year #=> 2012-12-31 23:59:59 UTC
However, if you go back in time things get strange.
date = '1399-3-2'.to_time(:utc) #=> 1399-03-02 00:00:00 UTC
date.end_of_year #=> 1399-12-23 23:59:59 UTC
23rd of December? Shouldn't that be 31st?
It's not even consistent:
date = '0000-3-2'.to_time(:utc) #=> 0000-03-02 00:00:00 UTC
date.end_of_year #=> 0001-01-02 23:59:59 UTC
Um, the 2nd of January? OF THE NEXT YEAR? What is going on?
Also, are leap years taken into account by the object?
You could use DateTime instead:
date = '2012-3-2'.to_datetime #=> Fri, 02 Mar 2012 00:00:00 +0000
date.end_of_year #=> Mon, 31 Dec 2012 23:59:59 +0000
date = '1399-3-2'.to_datetime #=> Sun, 02 Mar 1399 00:00:00 +0000
date.end_of_year #=> Wed, 31 Dec 1399 23:59:59 +0000
date = '0000-3-2'.to_datetime #=> Tue, 02 Mar 0000 00:00:00 +0000
date.end_of_year #=> Fri, 31 Dec 0000 23:59:59 +0000
It's mora accurate, and you can format the output
I've did some digging. Here's what I found.
Let's begin with end_of_year:
def end_of_year
change(:month => 12).end_of_month
end
Which relies on change and end_of_month:
def end_of_month
last_day = ::Time.days_in_month(month, year)
last_hour{ days_since(last_day - day) }
end
The most interesting part is happening inside of days_since:
def days_since(days)
advance(:days => days)
end
The advance method is a bit more complex:
def advance(options)
unless options[:weeks].nil?
options[:weeks], partial_weeks = options[:weeks].divmod(1)
options[:days] = options.fetch(:days, 0) + 7 * partial_weeks
end
unless options[:days].nil?
options[:days], partial_days = options[:days].divmod(1)
options[:hours] = options.fetch(:hours, 0) + 24 * partial_days
end
d = to_date.advance(options)
time_advanced_by_date = change(:year => d.year, :month => d.month, :day => d.day)
seconds_to_advance = options.fetch(:seconds, 0) +
options.fetch(:minutes, 0) * 60 +
options.fetch(:hours, 0) * 3600
if seconds_to_advance.zero?
time_advanced_by_date
else
time_advanced_by_date.since(seconds_to_advance)
end
end
And he is the guy we're looking for :
# in rails console
time = '0000-01-01'.to_time(:utc) #=> 0000-01-01 00:00:00 UTC
time.advance(days: 1) #=> 0000-01-04 00:00:00 UTC
time.advance(days: 2) #=> 0000-01-05 00:00:00 UTC
time.advance(days: 3) #=> 0000-01-06 00:00:00 UTC
That's all for now. I will continue to dig.

Parsing NSString to NSDate using NSDateFormatter

I have such string: "Tue, 22 Oct 2013 1:59 pm EEST"
i am trying to set these parsing rules:
[formatter setDateFormat: #"EEE, dd MMM yyyy hh:mm a z"];
but this returns nil when i perform:
[formatter dateFromString: #"Tue, 22 Oct 2013 1:59 pm EEST"];
What's the right regular expression for such format?
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_GB"];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"EEE, dd MMM yyyy hh:mm a zzz"];
[formatter setLocale:locale];
NSDate* date = [formatter dateFromString:#"Tue, 22 Oct 2013 1:59 PM EEST"];
NSLog(#"date: %#",date);
O/P:-date: 2013-10-22 10:59:00 +0000
hh is a padded hour ("01" in your case) but your string doesn't have padded hours (just "1" ) so it should be just h instead.
You can see a practical examples of the different formatting components below (output from this GitHub gist). The date being formatted is 1987-08-27 15:24:03
format result
--------------
yy 87
yyyy 1987
M 8
MM 08
MMM Aug
MMMM August
dd 27
HH 15
hh 03 // <--.
h 3 // <--'--- note the difference
a PM
mm 24
m 24
ss 03
s 3

Resources