Emacs can insert the current date and time in a file and keep it updated automatically. See Time Stamps.

Add the following Lisp to your InitFile.

  (add-hook 'before-save-hook 'time-stamp)

By default, Emacs can update the time stamp for the following two formats if one exists in the first 8 lines of the file.

   Time-stamp: <>
   Time-stamp: " "

The default ‘time-stamp-pattern’ in Emacs is roughly this,

  "8/Time-stamp:[ \t]+\\\\?[\"<]+%:y-%02m-%02d %02H:%02M:%02S %u\\\\?[\">]"

The format starts with a number and then a slash. This is the number of lines to search from the beginning. It can be negative for the number of lines from the bottom of the file. After the slash, a regular expression will find the beginning of the time stamp in the file, followeed by the time format string which you provide and is based on the format more precisely described in GIT:time-format.el and not the Lisp function ‘format-time-string’. The pattern ends with another regular expression. It matches the end of the time stamp.

According to the documentation for this variable, it’s important to not change the default value for ‘time-stamp-pattern’ globally. It’s better to change it with FileLocalVariables.

For example, to maintain a time stamp for a string constant in the third line of a C program, add this section of file local variables found at the end of this C source file.

  #include <stdio.h>
  static char const *ts = "f.c 1993/11/09 17:40:15 eggert";
  int main()
    return printf("%s\n", ts) == EOF;
  /* Local Variables: */
  /* time-stamp-pattern: "4/static char const \\*ts = \"%f %:y/%02m/%02d %02H:%02M:%02S %u\";" */
  /* End: */

To add a time stamp to the end of an HTML file set the ‘time-stamp-pattern’ to search in the last eight lines (-8) for a paragraph element containing “Last modified”. Also, this inserts a universal date with the ‘time-stamp-time-zone’ variable.

     <p>Last modified: </p>
  <!-- Local Variables: -->
  <!-- time-stamp-pattern: "-8/<p>Last modified:[ \t]+%3a %3b %02H:%02M:%02S %Z %:y by %u</p>" -->
  <!-- time-stamp-time-zone: "UTC" -->
  <!-- End: -->

You might not want to pollute all your HTML files with the local variables. Customize HtmlMode to affect all your HTML files with the following hook.

  (eval-after-load 'sgml-mode
    '(add-hook 'html-mode-hook (lambda ()
                                 (set (make-local-variable 'time-stamp-pattern)
                                      "-8/<p>Last modified:[ \t]+%3a %3b %02H:%02M:%02S %Z %:y by %u</p>")
                                 (set (make-local-variable 'time-stamp-time-zone) "UTC"))))

See also:

          Sun Dec 04 13:46:31 2005 (Pacific Standard Time)

See also InsertingAndUpdatingDates for ways to insert dates in buffers manually.

CategoryCalendar CategoryEditing CategoryFiles