find the last accessed date of a json file in ruby

You can use the File class in Ruby to get the last access time of a file. Here's an example for a JSON file:

main.rb
require 'json'

filename = 'path/to/your/file.json'

# Get the last access time of the file
last_accessed_time = File.atime(filename)

# Convert the time to a more readable format
formatted_time = DateTime.parse(last_accessed_time.to_s).strftime('%d-%m-%Y %H:%M:%S')

puts "The file #{filename} was last accessed on #{formatted_time}."
336 chars
12 lines

In the code above, we first require the json module for working with JSON files.

We then specify the filename variable to the path of the JSON file you want to get the last access time for.

We use the File.atime method to get the last access time of the file in a format similar to the Ruby Time class.

Finally, we convert the time to a more readable format using DateTime.parse and strftime, and then output it to the console.

gistlibby LogSnag