How to insert current time in the emacs org-mode
Asked Answered
H

7

48

Is there a simple way to insert the current time (like TIME: [2012-07-02 Mon 16:44]) in the org-mode? In the manual there is a lot of stuff (clocks, deadlines, schedules), but most of them require entering the time manually.

Hysell answered 2/7, 2012 at 14:44 Comment(0)
H
66

C-u C-c . will insert a timestamp at point, though Org will still prompt you for a time (with the default being the current time).

More: "Creating timestamps" in the Org Mode manual.

Hawthorn answered 2/7, 2012 at 14:55 Comment(5)
If somehow key binding doesn't work you need to M-x org-time-stampWicket
Yes, I did see that in the manual. But why there's no respective M-x command for the C-u C-c . shortcut. The M-x org-time-stamp only inserts the date without time to the current cursor. How about current date with time command?Sike
Explanation of "prefix argument": The Org Manual says regarding the C-c . shortcut to insert a date: "With a prefix argument, it also adds the current time." By this the manual means what Jon Gauthier said: type C-u C-c . (<control>-<u> <control>-<c> <full stop>). The prefix argument is C-u (pressing the <control> and <u> keys at the same time once before the rest of your command sequence).Oman
Can time inserted in between [] instead of <>?Philcox
@Philcox yup, c-c !Inconvenient
C
29

In my installation, which is org-mode version 9, the following enters the current date and time without prompting anything

C-u C-u C-c .
Culliton answered 1/7, 2017 at 22:34 Comment(4)
Perhaps I should upgrade from version 8.2.10 because that neat trick doesn't work for me. :-(Marder
GNU Emacs 26.1 (build 1, x86_64-redhat-linux-gnu, GTK+ Version 3.23.2) of 2018-08-13 Org mode version 9.1.9 (9.1.9-elpa @ /home/wagner/.emacs.d/elpa/org-20180327/) C-u C-u C-c . works for me tooSidneysidoma
programmatically it's possible with (org-time-stamp-inactive (16))`Require
How to insert date-only timestamp without promtping?Wigfall
K
9
C-u C-c !

inserts an inactive timestamp with the current time such as:

[2018-05-08 Tue 00:30]
Kizzie answered 7/5, 2018 at 19:6 Comment(1)
this does not do anythingPhilcox
M
6

In emacs-lisp, you could use

(org-insert-time-stamp (current-time) t)

With default settings, it will generate a time stamp on the format

<2021-06-20 Sun 10:33>

If you want to access it from any emacs session, put

(defun insert-now-timestamp()
  "Insert org mode timestamp at point with current date and time."
  (interactive)
  (org-insert-time-stamp (current-time) t))

in your .emacs file. You may then call the function with M-x insert-now-timestamp.

Emacs 27.2, org mode 9.4.4.

Edit: I now realise this does the same thing as @anachronic's solution. I however leave it here for reference.

Mattock answered 20/6, 2021 at 8:37 Comment(0)
R
2

On my installation

C-u C-c .

inserts a date-with-time stamp

Righthander answered 2/7, 2012 at 14:55 Comment(0)
C
2

Just give you some other options:

  1. If you are using windows system, you can use AutoHotKey to achieve this.
  2. If you can install YASnippet for emacs, it also have the shortcut to insert current date and time.

These two options are very powerful tools, not for just insert date and time.

Chesson answered 3/7, 2012 at 7:22 Comment(0)
I
1

If a time already exists in the buffer and you want to replace it with the current time, use time-stamp.

Time stamps are often delimited. For example, the date indicated by #+DATE: within an Org header is bounded by "#+DATE:" and the end of the line. Set the time-stamp-start and time-stamp-end variables to the beginning and end delimiters.

In the example of "#+DATE:", this might look like:

;; ^      beginning of line
;; \\#    a hash character
;; \\+    a plus character    
;; [Dd]+  one or more 'D' or 'd' character (and similarly with 'a', 't', and 'e')
;; :      a colon character
;; [ ]+   one or more spaces
(setq time-stamp-start "^\\#\\+[Dd]+[Aa]+[Tt]+[Ee]+:[ ]+")

;; $  end of the line
(setq time-stamp-end "$")

Set time-stamp-format to control the how the inserted time stamp looks. For example, %Y-%02m-%02d is the format for a date like "1969-07-20".

You can then update all the matching time stamps with M-x time-stamp.

A neat trick is to use File Variables. For example, creating a section at the end of an Org file will automatically update the "#+DATE:" header to the current date before every save:

* File variables :noexport:
# Local Variables:
# time-stamp-start: "^\\#\\+[Dd]+[Aa]+[Tt]+[Ee]+:[ ]+"
# time-stamp-end: "$"
# time-stamp-format: "%Y-%02m-%02d"
# eval: (add-hook 'before-save-hook 'time-stamp)
# End:
Intermarriage answered 16/1, 2024 at 8:20 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.