25
\$\begingroup\$

Challenge

Weirdly, this hasn't been done yet: output the current date.

Rules

The date format you should follow is as follows:

YYYY-MM-DD

Where the month and day should be padded by zeroes if they are less than 10.

For example, if the program is run on the 24th May 2017, it should output

2017-05-24

The date can either be always in UTC or in the local date.

You must handle leaps years. i.e. in leap years, February has 29 days but 28 days in a normal year.

Winning

Shortest code in bytes wins.

\$\endgroup\$
  • 14
    \$\begingroup\$ Happy 10k rep ! \$\endgroup\$ – Rohan Jhunjhunwala May 24 '17 at 20:42
  • 1
    \$\begingroup\$ And congrats on the fastest growing thread I´ve ever seen. :D \$\endgroup\$ – Titus May 24 '17 at 20:59
  • \$\begingroup\$ @Titus You should have seen Hello, World! :D \$\endgroup\$ – Beta Decay May 24 '17 at 21:00
  • \$\begingroup\$ ... and in normal years February has 31 days of course \$\endgroup\$ – edc65 May 24 '17 at 21:05
  • \$\begingroup\$ @edc65 I should really have known that Feb is 29 days long on leap years :P \$\endgroup\$ – Beta Decay May 24 '17 at 21:21

60 Answers 60

1
\$\begingroup\$

q/kdb+, 18 16 bytes

Solution:

ssr[.z.d$:].".-"

Example:

q)ssr[.z.d$:].".-"
"2018-01-01"

Explanation:

Take the string representation of today's date, and replace the dots with dashes.

ssr[string .z.d;".";"-"] / fully ungolfed solution
ssr[string .z.d;;].".-"  / partially ungolfed solution
           .z.d          / current UTC date (2018.01.01)
    string               / convert to string ("2018.01.01")
ssr[           ;;]       / string-search-replace 
                  .".-"  / the two parameters
\$\endgroup\$
1
\$\begingroup\$

J, 16 bytes

6!:0'YYYY-MM-DD'

Try it online!

\$\endgroup\$
1
\$\begingroup\$

IBM PC DOS, 8088 Assembly, 55 bytes

B4 2A       MOV  AH, 2AH        ; get system date: CX=year, DH=month, DL=day 
CD 21       INT  21H            ; call DOS API
91          XCHG AX, CX         ; move year in CX to AX (dividend) 
B3 64       MOV  BL, 100        ; divide by 100 to separate first two and last two digits of year 
F6 F3       DIV  BL             ; AL = quotient (first two), AH = remainder (last two) 
8B C8       MOV  CX, AX         ; save AX to CX to display last two digits later 
E8 0123     CALL DISP_WORD      ; display binary value in AL 
8A C5       MOV  AL, CH         ; move last two digits to AL 
E8 0123     CALL DISP_WORD      ; display binary value in AL 
B8 0E2D     MOV  AX, 0E2DH      ; display separator and BIOS function 
50          PUSH AX             ; save for next time 
CD 10       INT  10H            ; call BIOS 
8A C6       MOV  AL, DH         ; move month in DH into AL for display 
E8 0123     CALL DISP_WORD      ; display binary value in AL 
58          POP  AX             ; restore separator and BIOS function 
CD 10       INT  10H            ; call BIOS 
8A C2       MOV  AL, DL         ; move day into AL, and fall through PROC for display 
        DISP_WORD PROC
D4 0A       AAM                 ; convert binary to BCD 
86 C4       XCHG AL, AH         ; convert endian, AL = first digit, AH = second digit 
05 3030     ADD  AX, '00'       ; convert BCD to ASCII char 
50          PUSH AX             ; save second digit for display later 
B4 0E       MOV  AH, 0EH        ; PC BIOS display char function 
CD 10       INT  10H            ; call BIOS 
58          POP  AX             ; restore digits 
86 C4       XCHG AL, AH         ; move second digit to AL 
B4 0E       MOV  AH, 0EH        ; PC BIOS display char function 
CD 10       INT  10H            ; call BIOS 
C3          RET                 ; PROC return / return to DOS    
        DISP_WORD ENDP

Output

A complete DOS program, run from the command line outputs current date to the screen.

enter image description here

This ended up being larger than I anticipated. The vast majority of this program is doing what ASM programmers hate doing most which is writing code to convert binary values to ASCII. For this one, I'm using (abusing) built-in binary-to-BCD instructions (AAM in this case) to create a printable ASCII version of the output.

Download and try PRDATE.COM.

\$\endgroup\$
0
\$\begingroup\$

Fourier, 33 bytes

|~D<10{1}{0o}Do|F5do`-`4d^F`-`3dF

Try it online!

Uses a function which adds the leading zero... So long :P

\$\endgroup\$
0
\$\begingroup\$

Python 2, 59 bytes

Oddly enough, I can't find an easier built-in... somebody else found it. ><

from datetime import*
print datetime.now().isoformat()[:10]

Try it online!

\$\endgroup\$
0
\$\begingroup\$

jq, 19 characters

(15 characters code + 4 characters command line options)

now|todate[:10]

Sample run:

bash-4.4$ jq -nr 'now|todate[:10]'
2017-05-24

Try in jq‣play

\$\endgroup\$
0
\$\begingroup\$

SAS, 31 bytes

%put %sysfunc(date(),yymmdd10.);
\$\endgroup\$
0
\$\begingroup\$

MATL, 6 bytes

Z'29XO

Try it online!

Explanation

Z'    % Push current date and time as a float
29    % Push 29
XO    % String representation of date and time with specified format.
      % Format 29 corresponds to 'yyyy-mm-dd'. Implicitly display
\$\endgroup\$
0
\$\begingroup\$

C#, 51 bytes

Console.Write(DateTime.Now.ToString("yyyy-MM-dd"));
\$\endgroup\$
  • 2
    \$\begingroup\$ You can save 7 bytes by using string interpolation - Console.Write($"{DateTime.Now:yyyy-MM-dd}"); \$\endgroup\$ – Chris May 25 '17 at 7:28
  • \$\begingroup\$ This is just a code snippet, you need to wrap it in a function/program. You also need to fully qualify the classes or add the appropriate usings. \$\endgroup\$ – TheLethalCoder May 25 '17 at 10:40
  • 1
    \$\begingroup\$ Or look at @TheLethalCoder example on making a function that returns it rather than writing to standard output, most questions allow this. \$\endgroup\$ – LiefdeWen May 25 '17 at 13:04
  • \$\begingroup\$ @Chris Thanks! I didn't know that. \$\endgroup\$ – Micah Epps May 25 '17 at 14:01
0
\$\begingroup\$

Pyth, 20 bytes

j\-+.d3m.[\02`.d+4d2

Try it here.

\$\endgroup\$
0
\$\begingroup\$

C, 104 79 61 bytes

Saved 18 bytes thanks to hvd.

n,b[99];f(){time(&n);strftime(b,99,"%F",gmtime(&n));puts(b);}
\$\endgroup\$
  • 1
    \$\begingroup\$ "strftime segfaults if you don't #include <time.h>." -- Not on all implementations. You could get rid of that line and save some bytes by simply choosing an implementation where it works. (Most x86-32 Linux distros probably work.) \$\endgroup\$ – hvd May 25 '17 at 8:36
0
\$\begingroup\$

VBA, 26 Bytes

Anonymous VBE Immediate window function that outputs the current date in the specified format, regardless of system settings, to the VBE immediate window

?Format(Now,"YYYY-MM-DD")
\$\endgroup\$
0
\$\begingroup\$

PowerShell, 16 bytes

date -u %Y-%m-%d

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Haskell, 55 bytes

import Data.Time
m=getCurrentTime>>=putStr.take 10.show

Not exactly the shortest answer (even the Java answer is shorter!)

Explanation

import Data.Time                            --put the function getCurrentTime into scope
m=getCurrentTime>>=putStr.take 10.show
m=                                          --define a variable/function m
  getCurrentTime                            --get the current time wrapped in the IO type
                >>=                         --feed the current time into the following function
                                  show      --display the time as a string
                          take 10.          --take the first ten characters of that to get the date
                   putStr.                  --print it
\$\endgroup\$
0
\$\begingroup\$

Objective-C, 27 bytes

NSLog(@"%@",[NSDate date]);
\$\endgroup\$
  • 1
    \$\begingroup\$ Even shorter: NSLog(@"%@",[NSDate new]); \$\endgroup\$ – JAL Aug 8 '17 at 20:54
0
\$\begingroup\$

Python, 39 bytes

Beats this answer by 1 byte.

from datetime import*;str(date.today())

This won't actually work when put in a file and executed, but it does work when you paste this in a python shell. However, it works if you have this in a file and then call that file's __main__ function, so I don't know if this is allowed by the rules. I'm going to assume that it is.

\$\endgroup\$
0
\$\begingroup\$

Noether, 8 bytes

6D0 10SP

Try it here!

Explanation:

6  - Push the number 6 onto the stack
D  - Pop the mode number off the top of the stack and push the ISO date string (Mode 6)
0  - Push the number 0 onto the stack
10 - Push the number 10 onto the stack
S  - Pop the three items, A, B and C off the top of the stack and slice the string, A, from B to C
P  - Print the item on the top of the stack

Alternatively, you could use the following method (also for 8 bytes):

6D"T"^#P

Try it here!

Explanation:

6   - Push 6 onto the stack
D   - Pop the mode number off the top of the stack and push the ISO date string (Mode 6)
"T" - Push the string "T" onto the stack
^   - Pop two strings A and B off the stack and split A by B
#   - Pop the top item off the stack
P   - Print the item on the top of the stack
\$\endgroup\$
0
\$\begingroup\$

APL (Dyalog), 17 bytes

'-'@5 8⍕1E3⊥3↑⎕TS

Try it online!

⎕TS Y M D h m s ms

3↑ take the first three

1E3⊥ evaluate in base-1000

 make into string

'-'@5 8 substitute a dash at positions five and eight

\$\endgroup\$
0
\$\begingroup\$

Windows batch, 166 144 136 107 bytes

@set X=
@for /f "skip=1" %%x in ('wmic os get localdatetime')do @set X=%%x
@echo %X:~0,4%-%X:~4,2%-%X:~6,2%

Outputs local time

\$\endgroup\$
0
\$\begingroup\$

Common Lisp, 86 chars

(multiple-value-bind(a b c d e f)(get-decoded-time)(format t"~4a-~2,'0d-~2,'0d"f e d))

Good old verbose Common Lisp!

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Funky, 18 bytes

@os.date"%Y-%m-%d"

Try it online!

\$\endgroup\$
0
\$\begingroup\$

vim, 18

i®=strftime("%F")↵

where ® and are Ctrl-R and Enter.

\$\endgroup\$
0
\$\begingroup\$

AWK, 17 bytes

$0=strftime("%F")

Try it online!

\$\endgroup\$
0
\$\begingroup\$

jamal, 32 characters

{@format date=YEAR-0M-0D}{@date}

(Only interesting part is jamal's unusual date format specifiers. For example YYYY would mean YY twice, where YY is the year as in Perl, since 1900.)

Sample run:

bash-4.4$ jamal.pl date.jam 
2018-03-19
\$\endgroup\$
0
\$\begingroup\$

Gema, 40 characters

\A=@subst{*\\/*\\/*=\$3-\*-\*;@date}@end

Sample run:

bash-4.4$ gema '\A=@subst{*\\/*\\/*=\$3-\*-\*;@date}@end'
2018-03-19
\$\endgroup\$
0
\$\begingroup\$

Zsh, 15 bytes

Who needs date?

print -P %D{%F}

Try it online!

If we assume the program will only be run in current century, we can save 2 bytes:

print -P 20%D
\$\endgroup\$
0
\$\begingroup\$

Japt, 5 bytes

AîKs3

Try it

A         :10
 î        :Slice the following to that length
  K       :  Current date & time
   s3     :  Convert to ISO string
\$\endgroup\$
0
\$\begingroup\$

05AB1E, 16 13 bytes

Saved 3 bytes thanks to Kevin Cruijssen

žgžfže)T‰J'-ý

Try it online!

Explanation

žgžfže)         # push [year, month, day]
       T‰       # divmod each by 10
         J      # join each div and mod result together
          '-ý   # join year-month-day on "-"
\$\endgroup\$
  • \$\begingroup\$ It's a shame that žgžfže‚т+€¦`'-ý doesn't work. \$\endgroup\$ – Erik the Outgolfer May 25 '17 at 12:45
  • \$\begingroup\$ 13 bytes \$\endgroup\$ – Kevin Cruijssen Mar 25 at 14:20
  • 1
    \$\begingroup\$ @KevinCruijssen: The divmod trick being useful again. I'm definitely going to have to remember that ;) \$\endgroup\$ – Emigna Mar 25 at 15:04
0
\$\begingroup\$

Forth (gforth), 52 bytes

: x 0 <# # # #> ." -"type ; : f time&date 1 .r x x ;

Try it online!

Code Explanation

\ x prints a dash followed by a 2-digit number with leading zeros
: x               \ start a new word definition
  0 <# # # #>     \ convert number to double-precision, then converts to a string with leading 0
  ." -"type       \ print "-" followed by the string from the previous command
;                 \ end the word definition

: f               \ start a new word definition
  time&date       \ forth built-in to get the components of a date/time (year on top)
  1 .r            \ print the year (with no space appended)
  x               \ print a dash, followed by the month
  x               \ print a dash, followed by the day
;                 \ end word definition
\$\endgroup\$
0
\$\begingroup\$

Perl 6, 12 bytes

say now.Date

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.