# How to get the date of a file?

**URL:** <https://discourse.julialang.org/t/how-to-get-the-date-of-a-file/44701>\
**Category:** New to Julia\
**Created:** [August 10, 2020, 5:11pm UTC](https://discourse.julialang.org/t/how-to-get-the-date-of-a-file/44701 "2020-08-10T17:11:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jerpic](https://avatars.discourse-cdn.com/v4/letter/j/65b543/32.png) [@jerpic](https://discourse.julialang.org/u/jerpic)\
**Post date:** [August 10, 2020, 5:11pm UTC](https://discourse.julialang.org/t/how-to-get-the-date-of-a-file/44701/1 "2020-08-10T17:11:29Z")

</div>

Hello,

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?

Regards,

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [August 10, 2020, 6:00pm UTC](https://discourse.julialang.org/t/how-to-get-the-date-of-a-file/44701/2 "2020-08-10T18:00:23Z")

</div>

`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](https://en.wikipedia.org/wiki/Unix_time) of January 1, 1970.

---

<div class="post-metadata">

**Author:** ![Eben60](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eben60/32/13475_2.png) [@Eben60](https://discourse.julialang.org/u/Eben60)\
**Post date:** [August 10, 2020, 7:12pm UTC](https://discourse.julialang.org/t/how-to-get-the-date-of-a-file/44701/3 "2020-08-10T19:12:31Z")

</div>

> [@stevengj](#):
>
> `mtime` returns `0.0` for files that don’t exist

But why not error or missing/NaN/nothing ?

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [August 10, 2020, 8:01pm UTC](https://discourse.julialang.org/t/how-to-get-the-date-of-a-file/44701/4 "2020-08-10T20:01:32Z")

</div>

> [@Eben60](#):
>
> But why not error or missing/NaN/nothing ?

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.

---

<div class="post-metadata">

**Author:** ![jerpic](https://avatars.discourse-cdn.com/v4/letter/j/65b543/32.png) [@jerpic](https://discourse.julialang.org/u/jerpic)\
**Post date:** [August 11, 2020, 7:15am UTC](https://discourse.julialang.org/t/how-to-get-the-date-of-a-file/44701/5 "2020-08-11T07:15:54Z")

</div>

Hello Stevengj,

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

Here is my code before the fix:

```julia
 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);

        end

      end

    end

  end

```

After your reply, I changed this line:

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

to

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

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

Best Regards,

---

<div class="post-metadata">

**Author:** ![alex-s-gardner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alex-s-gardner/32/30210_2.png) [@alex-s-gardner](https://discourse.julialang.org/u/alex-s-gardner)\
**Post date:** [October 20, 2022, 3:58pm UTC](https://discourse.julialang.org/t/how-to-get-the-date-of-a-file/44701/6 "2022-10-20T15:58:06Z")

</div>

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

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