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"
Related
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!)
}
I'm extracting a date from a zip file, if the calendar is Japanese, it extract this string:
[R 2/03/13 19:00:17]
that is 13-mar-2020 19:00:17
I found on iOS documentation that dateFormatter.locale = Locale(identifier: "ja_JP")is printing:
// Japanese Locale (ja_JP)
dateFormatter.locale = Locale(identifier: "ja_JP")
print(dateFormatter.string(from: date)) // 2001/01/02
By searching on internet, I think the "R" is meaning Reiwa Era.
Do exist a dateFormat that could give the year?
Something like:
let stringJap = "[R 2/03/13 19:00:17]"
let stringJapDate = stringJap.replacingOccurrences(of: "[", with: "").replacingOccurrences(of: "]", with: "")
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "ja_JP")
dateFormatter.dateFormat = "? y-MM-dd HH:mm:ss" // the "?" means if is existing a character to describe Era in Japanase
let date = dateFormatter.date(from:stringJapDate)!
I'd avoid to build a switch case to extract the year by following the table below.
I found on internet this Era conversion:
2019 - Present ~ Reiwa Era
Western Calendar Japanese Calendar Western Calendar Japanese Calendar
2020 Reiwa 2 2019 Reiwa 1 / Heisei 31
1989 - 2019 ~ Heisei Era
Western Calendar Japanese Calendar Western Calendar Japanese Calendar
2019 Reiwa 1 / Heisei 31
2018 Heisei 30 2003 Heisei 15
2017 Heisei 29 2002 Heisei 14
2016 Heisei 28 2001 Heisei 13
2015 Heisei 27 2000 Heisei 12
2014 Heisei 26 1999 Heisei 11
2013 Heisei 25 1998 Heisei 10
2012 Heisei 24 1997 Heisei 9
2011 Heisei 23 1996 Heisei 8
2010 Heisei 22 1995 Heisei 7
2009 Heisei 21 1994 Heisei 6
2008 Heisei 20 1993 Heisei 5
2007 Heisei 19 1992 Heisei 4
2006 Heisei 18 1991 Heisei 3
2005 Heisei 17 1990 Heisei 2
2004 Heisei 16 1989 Heisei 1 / Showa 64
Any help?
Thanks
I use the SwiftDate library but I can't transform my string to date. This code return null
let date = "Tue, 19 Sep 2017 15:43:57 GMT".date(format: .custom("EEE, dd MMM yyyy HH:mm:ss zzz"))
Would anyone know why?
This code works now :
let region = Region(tz: TimeZoneName.gmt, cal: CalendarName.gregorian, loc: LocaleName.englishUnitedStates)
let date = "Tue, 19 Sep 2017 15:43:57 GMT".date(format: .custom("EEE, dd MMM yyyy HH:mm:ss zzz"), fromRegion: region)
Thank you for your help!
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.
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)