find the creation date of a json file in swift

You can use FileAttributeKey.creationDate to get the creation date of a file, and JSONSerialization to parse the JSON file. Here's an example code snippet:

main.swift
let fileManager = FileManager.default
let url = URL(fileURLWithPath: "/path/to/file.json")

do {
    let attributes = try fileManager.attributesOfItem(atPath: url.path)
    let creationDate = attributes[.creationDate] as? Date
    
    let jsonData = try Data(contentsOf: url)
    let jsonObject = try JSONSerialization.jsonObject(with: jsonData, options: [])
    // Process the jsonObject as needed
} catch {
    print("Error: \(error)")
}
441 chars
14 lines

In the attributes dictionary, the value for the .creationDate key is an optional Any object, so you'll need to cast it to a Date object before using it.

Note that the date formatter is not necessary in this case since creationDate is already a Date object. However, you may want to format the date string in a specific way for display purposes.

gistlibby LogSnag