How to get last-accessed date and time of file in Go?

golang get file creation time
golang time
convert date time to string golang
golang get file time
golang compare time
golang delete file
go time after
golang access time

Does anyone know how to check for a file access date and time? The function returns the modified date and time and I need something that compares the accessed date time to the current date and time.

You can use os.Stat to get a FileInfo struct which also contains the last access time (as well as the last modified and the last status change time).

info, err := os.Stat("example.txt")
if err != nil {
     // TODO: handle errors (e.g. file not found)
// info.Atime_ns now contains the last access time
// (in nanoseconds since the unix epoch)

After that, you can use time.Nanoseconds to get the current time (also in nanoseconds since the unix epoch, January 1, 1970 00:00:00 UTC). To get the duration in nanoseconds, just subtract those two values:

duration := time.Nanoseconds() - info.Atime_ns

Can I get the last access time from a file? - Getting Help, How I can get the last access date of a file? From what I see in the " fileinfo " structure does not exist when this property . Thanks in advance! How To Add Access Time Stamps To All Of Your Files In Windows 7. 1. Click the Start Menu Orb and then Search for cmd.. Once that comes up Right-Click cmd.exe and Select Run as administrator.

Alternatively, after the Stat you can also do


Also you can use Format() on it, should you need it eg for a webserver


Golang : Change a file last modified date and time, Golang : Change a file last modified date and time ( "fmt" "os" "time" ) func main () { filename := "binary.file" // get last modified time file, err := os. ctime is the inode or file change time. The ctime gets updated when the file attributes are changed, like changing the owner, changing the permission or moving the file to an other filesystem but will also be updated when you modify a file. The file ctime and atime are OS dependent.

By casting os.FileInfo to *syscall.Stat_t:

package main

import ( "fmt"; "log"; "os"; "syscall"; "time" )

func main() {
    for _, arg := range os.Args[1:] {
        fileinfo, err := os.Stat(arg)
        if err != nil {
        atime := fileinfo.Sys().(*syscall.Stat_t).Atim
        fmt.Println(time.Unix(atime.Sec, atime.Nsec))

How-To Enable Last Access Time Stamp For Files and Folder In , Anyway, a few things to keep in mind before we get started with this Tutorial: 1) Enabling or Disabling this won't make your Accessed date vanish� Get Last Access time of a file using os.stat () To get the last access time from os.stat_result object, access the property ST_ATIME, that contains the time of most recent access in seconds. Then we can covert that to readable format using time.ctime i.e.

Python : How to get Last Access & Creation date time of a file , stat(). To get the last access time from os.stat_result object, access the property ST_ATIME, that contains the time of most recent access in� You can parse the output of dir /b /ta [filename] using FOR /F and use the ~t delimiter to (in theory) get the last-accessed time like this for /f %%A in ( ' dir /b /TA "d:\path\filename.ext" ' ) do set accesstime=%%~tA

last time file opened, So there you go. There is Also, note that access times are by inode, not by filename, so if you have ls -ltu list all the files, showing and sorting by access time. In File Explorer, click on Quick Access in the left pane. This should show recent folders. If you right click on Quick Access and select >Options, under Privacy are settings to show recent files. And see this article for your jump lists.

Reading File Dates, st_atime returns the date/time when the file was last accessed, which includes st_ctime returns the date/time the file's status was last changed by a user. here on your site so I thought I'd get the book and have a go with it. The last time the file was actually changed and then saved. This date and time will be updated even if the file is opened, viewed and saved as itself with the same filename and no changes being made to the actual files data. Last Accessed The last time the file was viewed, but not changed.

  • This hasn't been correct since Go 1.0. You can get atime in a non-portable way by looking into FileInfo's Sys field (the type of that depends on GOOS).
  • time.Nanoseconds() will not tell time since epoch. It just tells the offset wrt current second. Correct way is time.Now().Unix(), but this will give in seconds...
  • What about time.Now().UnixNano()
  • The question asks about access time, not mod-time.
  • I googled the question you answered, so this answer gets a +1 from me. Thank you!