0

I am having trouble generating Date objects that match the json output. In http://chartapi.finance.yahoo.com/instrument/1.0/aapl/chartdata;type=quote;range=1y/json - the dates are in the following format "Date": 20151013. In order to get 2015-10-13, first I use Alamofire & SwiftJSON in my API call and JSON parsing. Here are some relevant lines from my code:

let dateInteger = subJson["Date"].int
if dateInteger != nil {
  let editedDateInt = dateInteger!
  let dateString = NSMutableString(string: "\(editedDateInt)")
  dateString.insert("-", at: 4)
  dateString.insert("-", at: 7)
chartpoint.date = Formatters.sharedInstance.dateFromString(key: dateString as String) }

// date extension
public class Formatters {
    public static let sharedInstance = Formatters()
private let dateKeyFormatter = DateFormatter()
init() { dateKeyFormatter.dateFormat = "yyyy-MM-dd" }

public func dateFromString(key: String?) -> Date? {
  guard let key = key else { return nil }
  return dateKeyFormatter.date(from: key) }

Problem is the output came up Optional(2015-10-12 16:00:00 +0000). Not quite 2015-10-13 that I was expecting. How do I fix this? Is this related to user's Locale or TimeZone?

Rajan Maheshwari
  • 14,465
  • 6
  • 64
  • 98
ckraider
  • 335
  • 5
  • 19
  • Your code is fine. Why do you think you have an issue? You parsed the string `2015-10-13` into a `Date?` instance. You then print that `Date?` and it shown the same date. – rmaddy Oct 12 '16 at 14:33
  • @EricAya I made the changes you suggested. Still the same output. 2015-10-12 16:00:00 +0000 ...off by a few hours. – ckraider Oct 12 '16 at 14:34
  • @rmaddy The output is not quite exact. 2015-10-12 16:00:00 +0000. Trying to make it precise and in the yyyy-MM-dd format to create a candlestick chart. – ckraider Oct 12 '16 at 14:36
  • If you want `yyyy-MM-dd` then why convert the string (which is already in that format) into a `Date`? A `Date` doesn't have a format. It represents a specific point in time. You convert that into a specific format by using a `DateFormatter` and getting a `String`. – rmaddy Oct 12 '16 at 14:38

1 Answers1

1

Check this out

Swift 3.0

let dateFromServer = "20151013"    
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyyMMdd"
dateFormatter.timeZone = TimeZone(abbreviation: "GMT")
let date = dateFormatter.date(from: dateFromServer)
dateFormatter.dateFormat = "yyyy-MM-dd"
let finalDate = dateFormatter.string(from: date!)
print(finalDate)

Output:

2015-10-13

As suggested by @rmaddy changed the format style form YYYY to yyyy.

Rajan Maheshwari
  • 14,465
  • 6
  • 64
  • 98
  • 1
    This is wrong. Do not use `YYYY`, use `yyyy` for the year. And do not set the time zone unless you knowingly understand why you are setting it to UTC. – rmaddy Oct 12 '16 at 14:39
  • @rmaddy As suggested by you I ll update. But can you tell the difference – Rajan Maheshwari Oct 12 '16 at 14:39
  • No, `YYYY` and `yyyy` do not give the same value all year. They are the same for many dates but not all. – rmaddy Oct 12 '16 at 14:41
  • @rmaddy Mentioned you. Thanks. I ll read more about this. I am quite weak in Dates. :) – Rajan Maheshwari Oct 12 '16 at 14:44
  • See for example http://stackoverflow.com/questions/15133549/difference-between-yyyy-and-yyyy-in-nsdateformatter (or read about "week-based calendar" in the Unicode date formatter documentation). YYYY would for example give a wrong result for "20160101". – Martin R Oct 12 '16 at 14:46
  • Also sometimes `NSCalender.current` and `Gregorian` gives weird results. – Rajan Maheshwari Oct 12 '16 at 14:49
  • Well my output needs to be in Date format for charting purpose. When I tried to download the date from server as a string before DateFormatter, I got nil. The only way it works is to download it as an integer, convert it to nsmutable string and insert "-" as I shown above. Adding GMT does work. – ckraider Oct 12 '16 at 14:56