What's the Date?

26

5

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.

Beta Decay

Posted 2017-05-24T20:16:11.143

Reputation: 21 478

14Happy 10k rep ! – Rohan Jhunjhunwala – 2017-05-24T20:42:30.787

1And congrats on the fastest growing thread I´ve ever seen. :D – Titus – 2017-05-24T20:59:00.570

@Titus You should have seen Hello, World! :D – Beta Decay – 2017-05-24T21:00:21.083

... and in normal years February has 31 days of course – edc65 – 2017-05-24T21:05:08.083

@edc65 I should really have known that Feb is 29 days long on leap years :P – Beta Decay – 2017-05-24T21:21:58.660

Finally a golf where golfing languages cant perform – Sivaprasath Vadivel – 2017-05-25T06:04:35.260

RIP Python needing the word datetime twice – WhatsThePoint – 2017-05-25T08:38:44.280

In TIO, if I use p in Ruby it surrounds the output in quotes, but the actual date is in the right format. Is this okay? – snail_ – 2017-05-25T12:32:18.287

@snail_ Do you have a link to this? – Beta Decay – 2017-05-25T13:08:04.683

@BetaDecay here: https://tio.run/nexus/ruby#@1@gEJKZm6qXl1@uV1xSlFYC5Kiruqn//w8A

– snail_ – 2017-05-25T13:09:33.527

@snail_ That's fine – Beta Decay – 2017-05-25T13:10:11.987

A codegolf challenge where practical languages can beat golfing languages? Amazing! – KSmarts – 2017-05-25T13:44:55.533

Surprised no one commented on this, but: that's not "ISO8601 date format". ISO8601 is a whole class of valid date formats, some of which are pretty whacky, like -1432198. – Steve Bennett – 2017-06-05T07:37:35.117

@SteveBennett I see. I've edited accordingly – Beta Decay – 2017-06-05T08:01:39.467

Answers

12

Bash, 16 7 bytes

-8 bytes thanks to Neil (and fergusq) (no pipe required to output)
-1 byte thanks to 12431234123412341234123 (use the built-in option with flag -I!)

date -I

Try it online!

Jonathan Allan

Posted 2017-05-24T20:16:11.143

Reputation: 67 804

2Why the echo $(...)? – Neil – 2017-05-24T20:28:22.243

Does it not need it to output? – Jonathan Allan – 2017-05-24T20:29:10.700

6date outputs already, the output is piped to stdout. You don't need to pipe it to echo, which pipes it to stdout. Try it online! – fergusq – 2017-05-24T20:30:36.313

But even if you really want to use echo, then at least echo \date +%F``. – manatwork – 2017-05-24T20:31:56.950

@manatwork Thanks, I am totally new to bash, it really is alien to me. – Jonathan Allan – 2017-05-24T20:33:07.487

5why not use -I ? – 12431234123412341234123 – 2017-05-25T07:10:47.850

@12431234123412341234123 Indeed -I will work (note to self: rtfm). There is also an entry on the question I linked to with that! Thanks. – Jonathan Allan – 2017-05-25T17:21:08.993

10

PHP, 17 bytes

<?=date('Y-m-d');

Titus

Posted 2017-05-24T20:16:11.143

Reputation: 13 814

1

Actually, YYYY-MM-DDThh:mm:ss+hh:mm is also an ISO 8601 formatted date and these 11 bytes: <?=date(c); print the whole stuff. ;)

– Titus – 2017-05-24T20:58:04.747

<?=strstr(date(c),T,1); to give the c format a little chance – Jörg Hülsermann – 2017-05-24T21:51:07.413

7

Bash, 15 bytes

printf '%(%F)T'

Sample run:

bash-4.4$ printf '%(%F)T'
2017-05-24

Try it online!

manatwork

Posted 2017-05-24T20:16:11.143

Reputation: 17 865

Even better than I found on SO, was unaware of T. – Jonathan Allan – 2017-05-24T20:24:12.393

@JonathanAllan, it was added only in Bash 4.2 and until Bash 4.3 it required an argument to format. – manatwork – 2017-05-24T20:28:47.280

7

Japt, 6 bytes

Ks3 ¯A

Try it online!

Explanation:

Ks3 ¯A
K       // New Date()
 s3     // .toISOString()
    ¯A  // .slice(0,10)

Oliver

Posted 2017-05-24T20:16:11.143

Reputation: 7 160

5

JavaScript (ES6), 34 bytes

_=>new Date().toJSON().split`T`[0]

f=

_=>new Date().toJSON().split`T`[0]

console.log(f());

Johan Karlsson

Posted 2017-05-24T20:16:11.143

Reputation: 670

Hmm, same length as _=>new Date().toJSON().slice(0,10) – Steve Bennett – 2017-05-25T07:51:29.917

4

SQLite, 13 characters

select date()

Good boy, SQLite. Other SQL dialects usually need either current_date or date(now()).

Sample run:

bash-4.4$ sqlite3 <<< 'select date()'
2017-05-24

manatwork

Posted 2017-05-24T20:16:11.143

Reputation: 17 865

1

MySQL has curdate

– Titus – 2017-05-28T22:06:57.623

4

Mathematica, 20 bytes

DateString@"ISODate"

J42161217

Posted 2017-05-24T20:16:11.143

Reputation: 15 931

9Sometimes I come into these challenges not to see if Mathematica has a built-in, but to see what it is – PunPun1000 – 2017-05-25T18:03:21.503

4

Perl 6,  14  12 bytes

Date.today.say

Try it

now.Date.say

Try it

Brad Gilbert b2gills

Posted 2017-05-24T20:16:11.143

Reputation: 12 713

3

Excel, 24 bytes

=TEXT(NOW(),"yyy-mm-dd")

Excel will still do a 4-digit year with only 3 y's.

Scott Milner

Posted 2017-05-24T20:16:11.143

Reputation: 1 806

2

Google sheets will add the "). Excel won't. It will add the ) on the end but only after prompting you so it would require additional user input and drive the score up again. 24 bytes is probably the shortest Excel answer unless we allow for regional settings using the ISO date format by default and that's up for debate.

– Engineer Toast – 2017-05-25T13:43:50.890

@EngineerToast Thanks. I'll fix that. – Scott Milner – 2017-05-25T22:56:29.767

2

R, 10 bytes

Sys.Date()

Try it online!

Giuseppe

Posted 2017-05-24T20:16:11.143

Reputation: 21 077

1Since sys.date already is a function, you don't have to add the (), so this is only 8 bytes :) – JAD – 2017-05-25T12:52:33.137

@JarkoDubbeldam not true, the () is necessary – D. Nelson – 2017-05-26T12:34:51.913

@D.Nelson https://codegolf.meta.stackexchange.com/questions/2419/default-for-code-golf-program-function-or-snippet Sys.Date is a function, so would suffice as solution. No need to explicitly call it.

– JAD – 2017-05-26T12:54:26.390

@JarkoDubbeldam that's normally the case but I think that since the challenge is to 'output the current date in ISO-8601 format' I actually need to output it. – Giuseppe – 2017-05-26T13:38:27.900

1Well yeah, and the last line of Sys.Date does the outputting for you. It is similar to when you have a solution that starts with function(x), you don't end that with an explicit call either. It's just how built-ins are scored. – JAD – 2017-05-26T13:53:08.017

2

Lua, 18 characters

print(os.date"%F")

Sample run:

bash-4.4$ lua -e 'print(os.date"%F")'
2017-05-24

Try it online!

manatwork

Posted 2017-05-24T20:16:11.143

Reputation: 17 865

2

Alice, 9 bytes

/oT\
@%;'

Try it online!

Explanation

I'll leave the exact control flow as an exercise to the reader, but the linearised code that is being run in Ordinal mode is:

%T'T%;o@

And here is what it does:

%   Split an implicit empty string around an implicit empty string. Really doesn't
    do anything at all.
T   Push the current datetime as a string like "2017-05-24T20:53:08.150+00:00"
'T  Push "T".
%   Split the datetime string around the "T", to separate the date from the time.
;   Discard the time.
o   Output the date.
@   Terminate the program.

One way this might be golfable is to reuse the % to terminate the program in Cardinal mode with a division by zero, but the only layout I've come up with is the following:

\;T
\%o'

But here, the % doesn't actually terminate the program, because we push 111 ('o) right beforehand so there's never a division by zero.

In principle it might also be possible to reuse % to get rid of the ;, since trying to split the date around the time will simply discard the time.

Martin Ender

Posted 2017-05-24T20:16:11.143

Reputation: 184 808

2

VBA, 5 25 bytes

?Date unpredictable, dependent on system short date settings

?Format(Now,"yyyy-mm-dd")

Output:

2017-05-25 

Maciej Lipinski

Posted 2017-05-24T20:16:11.143

Reputation: 47

6This answer is exceedingly volatile and by deafult, will not work on most computers as it depends entirely upon the user having their system short date format [under settings -> Date & Time in Win10] set to 'YYYY-MM-DD. The default format for this isM/D/YYYYand thus this for the output of theDatefunction is5/25/2017. To correct this your answer would need to be wrapped in aFormat` call. – Taylor Scott – 2017-05-25T13:44:10.843

2@TaylorScott to that end ?format(now,"yyyy-mm-dd") using now is shorter than date – Greedo – 2017-05-25T16:22:41.997

1@Greedo, that is correct. This is, to my knowledge, the shortest way to achieve this goal using VBA – Taylor Scott – 2017-05-25T18:16:22.643

1@TaylorScott thanks for pointing this out, I wasn't aware of the dependence on the system date settings. This date format is the default on my PC and in my country in general, so I was a little overconfident here. – Maciej Lipinski – 2017-05-26T08:45:51.897

2

SmileBASIC 3, 29 bytes

SB has a date string built in... but it's in the wrong format! It uses slashes instead of dashes, no good. Plus, being the self-respecting BASIC it is, there is no global replace function. I guess I have to do it myself...

D$=DATE$D$[4]="-
D$[7]="-
?D$

snail_

Posted 2017-05-24T20:16:11.143

Reputation: 1 982

2

Prolog (SWI), 46 bytes

f(X):-get_time(Y),format_time(atom(X),'%F',Y).

Try it online!

qwertxzy

Posted 2017-05-24T20:16:11.143

Reputation: 101

2Welcome to PPCG! – Martin Ender – 2018-01-02T10:59:33.120

1

CJam, 22 15 bytes

et3<{sY0e[}%'-*

Try it online!

-7 bytes thanks to Challenger5.

Explanation:

et                       Get array with [year,month,day,stuff...]
  3<                     Slice array to get [y,m,d] 
    {                    For each item do:
     s                     To string
      Y0e[                 add a 0 to the beginning of the string if it is shorter than 2 chars.
          }%             End for each
            '-*          Join the array with "-" as a separator

FrodCube

Posted 2017-05-24T20:16:11.143

Reputation: 539

You can use e[ (pad array) for 15 bytes: et3<{sY0e[}%'-* – Esolanging Fruit – 2017-05-24T20:35:43.537

@Challenger5 that's cool. Thanks – FrodCube – 2017-05-24T20:38:56.237

1

Crystal, 30 bytes 24 bytes 21 bytes

-6 thanks to Nick Clifford

-3 from looking at snail_'s answer in Ruby

p Time.now.to_s("%F")

Try it online!

TitusLucretius

Posted 2017-05-24T20:16:11.143

Reputation: 121

2I'm pretty sure you can use %F. Also, nice to see Crystal on PPCG! – Nick Clifford – 2017-05-24T20:38:17.533

1@Nick Clifford I saw it on tio and thought it had a cool name. – TitusLucretius – 2017-05-24T20:44:47.377

1

QBIC, 33 bytes

B=_D?_sB,-4|+@-`+_sB,2|+A+_sB,4,2

Explanation:

B=_D        Assign the system's date to B$
            This is in American mm-dd-yyyy format, so we'll need to do some reformatting
?_sB,-4|    PRINT substring B, take 4 chars from the right
 +@-`         plus the string literal "-", now A$
 +_sB,2|      plus the leftmost two chars
 +A           and A$ again
 +_sB,4,2     plus the middle part.

steenbergh

Posted 2017-05-24T20:16:11.143

Reputation: 7 772

1

Python 2,  53  40 bytes

-10 bytes thanks to Gábor Fekete (ISO-8601 is the default format for a date object)

from datetime import*
print date.today()

Try it online!

How?

datetime.date.today() will return a datetime.date object containing the local date information.

print will print a string representation of that object, this will call the object's __str__ function.

From the docs:

  • date.__str__(): For a date d, str(d) is equivalent to d.isoformat().

  • date.isoformat(): Return a string representing the date in ISO 8601 format, ‘YYYY-MM-DD’. For example, date(2002, 12, 4).isoformat() == '2002-12-04'.

Jonathan Allan

Posted 2017-05-24T20:16:11.143

Reputation: 67 804

1import datetime;print datetime.date.today() this is only 43 bytes and uses datetime. Why is the even shorter version deleted? – Gábor Fekete – 2017-05-25T12:05:15.130

@GáborFekete Thanks (I think you should really have posted, it's sufficiently different). The other answer was probably deleted because it did not print the correctly formatted string. I have added an explanation about why this does actually work (so long as one includes the print). Saved another three doing (the evil) import*. – Jonathan Allan – 2017-05-25T17:42:24.243

I wanted to but there was an another solution which was even shorter than mine but was deleted for some reason. – Gábor Fekete – 2017-05-26T08:39:28.670

...which has now been edited to include the print while deleted then undeleted. – Jonathan Allan – 2017-05-26T08:43:35.113

1

Go, 62 56 bytes

import."time"
func f()string{return Now().String()[:10]}

Try it online!

totallyhuman

Posted 2017-05-24T20:16:11.143

Reputation: 15 378

1

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

streetster

Posted 2017-05-24T20:16:11.143

Reputation: 3 635

1

Python 2, 40 bytes

from datetime import*;print date.today()

Wondercricket

Posted 2017-05-24T20:16:11.143

Reputation: 251

1

Powershell, 26 17 bytes

Date -f yyy-MM-dd

Thanks to @ConnorLSW for the 9 bytes.

Sivaprasath Vadivel

Posted 2017-05-24T20:16:11.143

Reputation: 139

you can save a lot here. date -f 'yyyy-MM-dd' – colsw – 2017-05-25T10:10:50.947

Thanks Man. I am new to Powershell. So, I tried my luck here – Sivaprasath Vadivel – 2017-05-25T10:13:43.460

@Shivaprasath V - no worries, check this thread for a couple of helpful posts with the more basic PS golfing tricks if you want.

– colsw – 2017-05-25T10:16:37.213

1

Oracle SQL, 46 bytes

SELECT TO_CHAR(SYSDATE,'YYYY-MM-DD') FROM DUAL

Khaled.K

Posted 2017-05-24T20:16:11.143

Reputation: 1 435

1

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 "-"

Emigna

Posted 2017-05-24T20:16:11.143

Reputation: 50 798

It's a shame that žgžfže‚т+€¦\'-ý` doesn't work. – Erik the Outgolfer – 2017-05-25T12:45:32.860

13 bytes – Kevin Cruijssen – 2019-03-25T14:20:27.383

1@KevinCruijssen: The divmod trick being useful again. I'm definitely going to have to remember that ;) – Emigna – 2019-03-25T15:04:31.907

1

C#, 46 45 bytes

_=>System.DateTime.Now.ToString("yyy-MM-dd");

Saved a byte thanks to @raznagul.

TheLethalCoder

Posted 2017-05-24T20:16:11.143

Reputation: 6 930

2Where do I download Sytem? – Erik the Outgolfer – 2017-05-25T13:09:40.823

6@EriktheOutgolfer It's a custom wrapper I created around the System namespace because I kept doing the same typo... – TheLethalCoder – 2017-05-25T13:12:05.097

4You know stuff you create locally isn't allowed on PPCG unless it's published before the question is asked. :P – Erik the Outgolfer – 2017-05-25T13:13:28.953

1@EriktheOutgolfer Damn, next time I'll create a shorter wrapper as well... – TheLethalCoder – 2017-05-25T13:14:49.180

1As leading zeroes for the year are not required the format string "yyy-MM-dd" will also work. – raznagul – 2017-05-26T10:37:11.037

1

Java 8, 26 32 bytes

()->java.time.LocalDate.now()+""

Fixed format thanks to Kevin Cruijssen

jaxad0127

Posted 2017-05-24T20:16:11.143

Reputation: 281

Wow, Java is actually short... I'd never have thought I'd see the day :D – Beta Decay – 2017-05-25T12:18:24.617

@BetaDecay It is short, but currently not complying to standard codegolf rules. It should be a function instead of a snippet, and the required imports should be counted as well. So it should be ()->java.time.LocalDate.now().toString() instead. Then again, you can golf .toString() to +"", so it becomes ()->java.time.LocalDate.now()+"" in total (32 bytes).

– Kevin Cruijssen – 2017-05-26T09:56:30.210

1

Ruby, 23 bytes

Prints the local time.

p Time.now.strftime'%F'

snail_

Posted 2017-05-24T20:16:11.143

Reputation: 1 982

1p Time.now.to_s[0..9] – manatwork – 2017-05-25T14:33:00.113

1

MATLAB/Octave, 25 15 bytes

datestr(now,29)

Try it online!


The built-in function now returns the current system date in a weirdy MATLAB serial format.

datestr formats the weirdy serial format into a string of a requested format - which is in this case 'YYYY-mm-dd'. It turns out that MATLAB has a list of predefined formats for datestr. ISO8601 is one of them and is represented by the number 29, which allows a saving of 10 bytes.

Tom Carpenter

Posted 2017-05-24T20:16:11.143

Reputation: 3 990

1

Rust, 84 Bytes

extern crate chrono;fn main(){print!("{}",chrono::Local::now().format("%Y-%m-%d"));}

Chad Baxter

Posted 2017-05-24T20:16:11.143

Reputation: 248

1

C++14, 143 bytes 139 bytes

#include <cstdio>
#include <time.h>
int main (){time_t t;char D[11];time(&t);strftime(D,sizeof D,"%Y-%m-%d",localtime(&t));printf("%s",D);}

Timtech pointed out to me that i don't need all spaces

Test me!

nobbele

Posted 2017-05-24T20:16:11.143

Reputation: 11

Can't you remove a few of those spaces? – Timtech – 2017-06-04T19:11:05.793

1

Swift, 13 bytes

print(Date())

Dimitrie-Toma Furdui

Posted 2017-05-24T20:16:11.143

Reputation: 139

1

J, 16 bytes

6!:0'YYYY-MM-DD'

Try it online!

FrownyFrog

Posted 2017-05-24T20:16:11.143

Reputation: 3 112

1

IBM PC DOS, 8088 Assembly, 55 53 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 
05 3030     ADD  AX, '00'       ; convert BCD to ASCII char 
50          PUSH AX             ; save second digit for display later 
86 C4       XCHG AL, AH         ; convert endian, AL = first digit, AH = second digit 
B4 0E       MOV  AH, 0EH        ; PC BIOS display char function 
CD 10       INT  10H            ; call BIOS 
58          POP  AX             ; restore digits
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 test PRDATE.COM.

640KB

Posted 2017-05-24T20:16:11.143

Reputation: 7 149

0

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!

totallyhuman

Posted 2017-05-24T20:16:11.143

Reputation: 15 378

0

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

Beta Decay

Posted 2017-05-24T20:16:11.143

Reputation: 21 478

0

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

manatwork

Posted 2017-05-24T20:16:11.143

Reputation: 17 865

0

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);}

MD XF

Posted 2017-05-24T20:16:11.143

Reputation: 11 605

1"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.) – hvd – 2017-05-25T08:36:01.650

0

SAS, 31 bytes

%put %sysfunc(date(),yymmdd10.);

J_Lard

Posted 2017-05-24T20:16:11.143

Reputation: 351

0

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

Luis Mendo

Posted 2017-05-24T20:16:11.143

Reputation: 87 464

0

C#, 51 bytes

Console.Write(DateTime.Now.ToString("yyyy-MM-dd"));

Micah Epps

Posted 2017-05-24T20:16:11.143

Reputation: 101

2You can save 7 bytes by using string interpolation - Console.Write($"{DateTime.Now:yyyy-MM-dd}"); – Chris – 2017-05-25T07:28:16.977

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. – TheLethalCoder – 2017-05-25T10:40:02.773

1Or look at @TheLethalCoder example on making a function that returns it rather than writing to standard output, most questions allow this. – LiefdeWen – 2017-05-25T13:04:59.227

@Chris Thanks! I didn't know that. – Micah Epps – 2017-05-25T14:01:32.960

0

Pyth, 20 bytes

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

Try it here.

Erik the Outgolfer

Posted 2017-05-24T20:16:11.143

Reputation: 38 134

0

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")

Taylor Scott

Posted 2017-05-24T20:16:11.143

Reputation: 6 709

0

Objective-C, 27 bytes

NSLog(@"%@",[NSDate date]);

Dimitrie-Toma Furdui

Posted 2017-05-24T20:16:11.143

Reputation: 139

1Even shorter: NSLog(@"%@",[NSDate new]); – JAL – 2017-08-08T20:54:57.370

0

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

stevefestl

Posted 2017-05-24T20:16:11.143

Reputation: 539

0

PowerShell, 16 bytes

date -u %Y-%m-%d

Try it online!

Andrei Odegov

Posted 2017-05-24T20:16:11.143

Reputation: 939

0

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

Beta Decay

Posted 2017-05-24T20:16:11.143

Reputation: 21 478

0

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

Generic Display Name

Posted 2017-05-24T20:16:11.143

Reputation: 365

0

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.

latias1290

Posted 2017-05-24T20:16:11.143

Reputation: 91

0

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

Adám

Posted 2017-05-24T20:16:11.143

Reputation: 37 779

0

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!

Renzo

Posted 2017-05-24T20:16:11.143

Reputation: 2 260

0

Funky, 18 bytes

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

Try it online!

ATaco

Posted 2017-05-24T20:16:11.143

Reputation: 7 898

0

vim, 18

i®=strftime("%F")↵

where ® and are Ctrl-R and Enter.

pacholik

Posted 2017-05-24T20:16:11.143

Reputation: 490

0

AWK, 17 bytes

$0=strftime("%F")

Try it online!

DarkHeart

Posted 2017-05-24T20:16:11.143

Reputation: 171

0

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

manatwork

Posted 2017-05-24T20:16:11.143

Reputation: 17 865

0

Gema, 40 characters

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

Sample run:

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

manatwork

Posted 2017-05-24T20:16:11.143

Reputation: 17 865

0

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

GammaFunction

Posted 2017-05-24T20:16:11.143

Reputation: 2 838

0

Japt, 5 bytes

AîKs3

Try it

A         :10
 î        :Slice the following to that length
  K       :  Current date & time
   s3     :  Convert to ISO string

Shaggy

Posted 2017-05-24T20:16:11.143

Reputation: 24 623

0

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

reffu

Posted 2017-05-24T20:16:11.143

Reputation: 1 361

0

Perl 6, 12 bytes

say now.Date

Try it online!

bb94

Posted 2017-05-24T20:16:11.143

Reputation: 1 831

0

Pyth, 19 bytes

++++.d3"-".d4"-".d5

In Pyth . allows you to access a number of extra syntactic goodies. d contains various date options which are accessed with .d#, where # is a number 0-9. 3 is for the current year, 4 is the current month, and 5 is for the current day.

Format for the challenge wants dashes between each element of the date so we append them. Pyth using Polish notation for all math operands, thus the ++++.

Ian Pringle

Posted 2017-05-24T20:16:11.143

Reputation: 11

Consider adding a short explanation of your code for those who don't know Pyth. Short code-only answers (like this one) tend to be automatically flagged as low quality. – mbomb007 – 2019-05-31T13:27:47.750

Thanks I'll do that! – Ian Pringle – 2019-05-31T15:40:29.800