Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

a command to insert today's date OR time (or both)?

3 views
Skip to first unread message

David Combs

unread,
Jan 8, 2011, 8:00:04 PM1/8/11
to
I've looked in emacs' *info*, its list of commands, etc,
and can find nothing.

Sure would be nice to be to easily to insert the current
date, time, or both.

Thanks!

David

Pascal J. Bourguignon

unread,
Jan 8, 2011, 10:30:15 PM1/8/11
to
dkc...@panix.com (David Combs) writes:

> I've looked in emacs' *info*, its list of commands, etc,
> and can find nothing.

C-u M-! date RET
Sun Jan 9 04:29:12 CET 2011

C-u M-: (calendar-current-date) RET
(1 9 2011)

> Sure would be nice to be to easily to insert the current
> date, time, or both.

Then write the command to do so.
Notice how each user will want to insert it in a different format.

--
__Pascal Bourguignon__ http://www.informatimago.com/
A bad day in () is better than a good day in {}.

Udyant Wig

unread,
Jan 9, 2011, 12:19:50 AM1/9/11
to
dkc...@panix.com (David Combs) writes:

Apart from what Pascal suggested, you could try these:

(defun insert-current-time ()
(interactive)
(destructuring-bind
(sec min hour day month year dow dst zone)
(decode-time)
(insert (format "%02d:%02d:%02d" hour min sec))))

(defun insert-current-date ()
(interactive)
(let ((days '((0 . Sunday)
(1 . Monday)
(2 . Tuesday)
(3 . Wednesday)
(4 . Thursday)
(5 . Friday)
(6 . Saturday)))
(months '((1 . Jan)
(2 . Feb)
(3 . Mar)
(4 . Apr)
(5 . May)
(6 . Jun)
(7 . Jul)
(8 . Aug)
(9 . Sep)
(10 . Oct)
(11 . Nov)
(12 . Dec))))
(destructuring-bind
(sec min hour day month year dow dst zone)
(decode-time)
(insert (format "%s, %d %s %d"
(rest (assoc dow days))
day
(rest (assoc month months))
year)))))

You could, of course, combine these as:

(defun insert-current-time-date ()
(interactive)
(insert-current-time)
(insert (format " "))
(insert-current-date))

Or whichever way you want them.

P.S. Suggested improvements gladly accepted.

Udyant Wig

unread,
Jan 9, 2011, 12:30:54 AM1/9/11
to
Udyant Wig <udy...@gmail.com> writes:

| dkc...@panix.com (David Combs) writes:
|
| | I've looked in emacs' *info*, its list of commands, etc,
| | and can find nothing.
| |
| | Sure would be nice to be to easily to insert the current
| | date, time, or both.
| |
| | Thanks!
| |
| | David

| <Reply elided>

You'll want to do a
(require 'cl)
before using that.

Sorry for the omission.

Gregor Zattler

unread,
Jan 9, 2011, 7:06:41 AM1/9/11
to help-gnu-emacs
Hi Pascal,
* Pascal J. Bourguignon <p...@informatimago.com> [09. Jan. 2011]:

> dkc...@panix.com (David Combs) writes:
>
>> I've looked in emacs' *info*, its list of commands, etc,
>> and can find nothing.
>
> C-u M-! date RET
> Sun Jan 9 04:29:12 CET 2011
>
> C-u M-: (calendar-current-date) RET
> (1 9 2011)
>
>> Sure would be nice to be to easily to insert the current
>> date, time, or both.
>
> Then write the command to do so.
> Notice how each user will want to insert it in a different format.

Stolen from some website:

(defun insert-date (prefix)
"Insert the current date. With prefix-argument, use ISO format. With
two prefix arguments, write out the day and month name. With
three (!) prefix arguments give date and time in ISO Format
and append day and month name in brackets"
(interactive "P")
(let ((format (cond
((not prefix) "%d.%m.%Y")
((equal prefix '(4)) "%Y-%m-%d")
((equal prefix '(16)) "%A, %d. %B %Y")))
(system-time-locale "de_DE"))
(insert (format-time-string format))))

(global-set-key (kbd "C-c d") 'insert-date)

Ciao, Gregor
--
-... --- .-. . -.. ..--.. ...-.-

Drew Adams

unread,
Jan 9, 2011, 9:23:11 AM1/9/11
to help-gn...@gnu.org

Drew Adams

unread,
Jan 9, 2011, 9:32:51 AM1/9/11
to David Combs, help-gn...@gnu.org
> I've looked in emacs' *info*, its list of commands, etc,
> and can find nothing.

You forgot Emacs Wiki: http://www.emacswiki.org/.

Just type "insert date" into the search field - voila!


Le Wang

unread,
Jan 9, 2011, 9:42:16 AM1/9/11
to Drew Adams, David Combs, help-gn...@gnu.org
You also forgot google.  The wiki link was the first result when I searched Emacs + this subject.

On Sun, Jan 9, 2011 at 10:32 PM, Drew Adams <drew....@oracle.com> wrote:
> I've looked in emacs' *info*, its list of commands, etc,
> and can find nothing.

You forgot Emacs Wiki: http://www.emacswiki.org/.

Just type "insert date" into the search field - voila!





--
Le

David Combs

unread,
Jan 20, 2011, 8:33:16 PM1/20/11
to
In article <mailman.18.1294584142...@gnu.org>,
Le Wang <l26...@gmail.com> wrote:
>-=-=-=-=-=-
>-=-=-=-=-=-
>[Alternative: text/html]
>-=-=-=-=-=-

THANKS EVERYONE!, for all the different ways of doing it!

David

ken

unread,
Jan 21, 2011, 6:36:10 AM1/21/11
to GNU Emacs List
A couple years ago I posted code to insert the current date and/or the
current time (and other functionality, e.g., yesterday's date,
tomorrow's date) to the emacs wiki. Oddly, that wiki page didn't turn
up in several different searches... I found it only after browsing the
Categories. To save you some time, it's here:

<http://www.emacswiki.org/emacs/Journal>

0 new messages