How to get the date of a file?


I’m very new to Julia. I try to get the last updated date of a list of file, for that I use the method “mtime”. The result for some files is correct but unfortunately for many I get always that date : 1970-01-01T00:00:00

Do you know why I got this result? Is there another approach to get the date of the file?


1 Like

Dates.unix2datetime(mtime(filename)) should work to get the modification date and time of a file, I think?

for many I get always that date : 1970-01-01T00:00:00

Maybe you are just passing an incorrect filename (or an incorrect path)? mtime returns 0.0 for files that don’t exist, which corresponds to the Unix epoch of January 1, 1970.


But why not error or missing/NaN/nothing ?

1 Like

This is a good question. Linux distributions (often) provide the stat command which has -c FORMAT flag and a %W time of file birth, seconds since Epoch; 0 if unknown format specification. So I would suppose it comes from there, but calling stat -c "%W" file_that_does_not_exist returns an error in my distribution (Arch Linux), so I am not sure why this decision was taken. Maybe opening a bug report is the best decision.

Hello Stevengj,

Many thanks for you reply, it helped me a lot.

Here is my code before the fix:

 for (root, dirs, files) in walkdir(".")
    for file in files
      filePath = joinpath(root, file);

      fileExtension = splitext(filePath)[2];

      if(fileExtension == ".txt")
        lastUpdatedTime = Dates.unix2datetime(stat(file).mtime);

        daysDifference = Dates.value(round(today - lastUpdatedTime, Dates.Day(1)));

        if(daysDifference >= daysLimit)
          println("File : " , filePath , " - Date : ", lastUpdatedTime , " - Days Difference : ", daysDifference);





After your reply, I changed this line:

lastUpdatedTime = Dates.unix2datetime(stat(file).mtime);


lastUpdatedTime = Dates.unix2datetime(stat(filePath).mtime);

And now I get the correct last updated date of each file.

Best Regards,

Here’s a generic 1-line solution to the question posed:

folder = “/path/to/directory”
last_modified = Dates.unix2datetime.(mtime.(joinpath.(folder,readdir(folder))))