Give me the first day of next month

1

Simple question!

Given a string that contains a date in ISO8601 format, print the first of the next month in ISO8601 format.

Example inputs:

2018-05-06

2019-12-20

Their respective outputs:

2018-06-01

2020-01-01

The rules:

  • Imports are free and don't count towards your score.
  • The date will be stored in a string called s.
  • The solution with the least characters, wins

ToonAlfrink

Posted 2019-12-13T10:24:28.483

Reputation: 1 122

9

Please avoid creating new tags unless they're really missing. We already have date and parsing. The beginner tag is an interesting idea, but I think that should be discussed in meta. On the other hand, you must include a tag telling what's the winning criterion. Is this code-golf?

– Arnauld – 2019-12-13T10:27:41.697

10I suggest removing The date will be stored in a string called s. and leave it up to the answerers to use any of the default I/O methods. Btw, reading from a pre-defined variable is not among those defaults. Also, not all languages have strings, and not all languages have variables. – Adám – 2019-12-13T10:31:15.813

1Do you mean the output should be stored in s, or that the input would be stored in s? – Kobe – 2019-12-13T11:00:17.800

1

@Arnauld There was discussion around that topic (a "beginner" or "easy" tag) quite a while ago with no clear consensus. IMO that's still the case because it's very subjective.

– AdmBorkBork – 2019-12-13T14:40:57.940

Within a day of posting this, 5 people flocked to close it as off-topic because it didn't contain an objective primary winning criterion. I thought "the least characters" was the default winning criterion? Isn't it quite petty to shut this down like that? – ToonAlfrink – 2019-12-19T14:26:28.503

1I'd say, the newly-added The solution with the least characters, wins could count as the winning criteria, but we prefer not to specify a variable that the input/output have to be stored inside – Shieru Asakoto – 2019-12-20T01:19:09.063

Answers

5

JavaScript (V8), 67, 57, 56 bytes

-10 thanks to Arnauld,
-1 joining template string and ternary

s=>([a,b]=s.split`-`,+a+!(b%=12))+`-${b++<9?'0'+b:b}-01`

Test cases:

f=s=>([a,b]=s.split`-`,+a+!(b%=12))+`-${b++<9?'0'+b:b}-01`

console.log(f('2018-12-20'))
console.log(f('2019-05-06'))

Kobe

Posted 2019-12-13T10:24:28.483

Reputation: 189

57 bytes (without assuming that the input is stored in s, as this unusual requirement will hopefully be removed) – Arnauld – 2019-12-13T11:12:03.600

I never knew you could do that with split, thanks for the input. Might I also ask what's happening with the -~b part? @Arnauld – Kobe – 2019-12-13T11:25:24.787

-~b is -(-b-1) and works on non-numeric values. But since b is already coerced to a number by the modulo, you can actually just use b+1 here. – Arnauld – 2019-12-13T11:28:06.677

@Kobe -~b is basically b+1 (or actually -(-b-1)). It can be useful to skip parenthesis sometimes, although it's not really necessary in this case, so you could change it to b+1 being more readable if you'd prefer. Relevant tip. You might also find Tips for golfing in JavaScript and Tips for golfing in <all languages> interesting to read through. Welcome to CGCC!

– Kevin Cruijssen – 2019-12-13T11:29:56.113

@KevinCruijssen Thanks for the comment and links, I'll definitely take a look at those soon :) – Kobe – 2019-12-13T11:31:59.757

4

Bash, 31, 24 bytes

7 bytes saved thanks to @manatwork and @nwellnhof

date -d${1%??}1month +%F

Try it online!

Nahuel Fouilleul

Posted 2019-12-13T10:24:28.483

Reputation: 5 582

Wow! date's parsing ability keeps amazing me. BTW, I think you can skip the “0”. – manatwork – 2019-12-13T10:57:02.730

2

Ruby 49 bytes

d=Date.parse(s);print Date.new(d.year,d.mon,1)>>1

Online repl

mariozski

Posted 2019-12-13T10:24:28.483

Reputation: 121

1

PHP, 57 bytes

[$y,$m]=explode('-', $s);$m=++$m%13?:!!++$y;$d="$y-$m-1";

Test cases:

<?php
function next_month($s) {
    [$y,$m]=explode('-', $s);
    $m=++$m%13?:!!++$y;
    return "$y-$m-1";
}

$s = "2018-05-06";
echo next_month($s); // Gives 2018-6-1

$s = "2019-12-20";
echo next_month($s); // Gives 2020-1-1

Kerkouch

Posted 2019-12-13T10:24:28.483

Reputation: 111

Consider using Try it online! to demonstrate your code.

– Neil – 2019-12-21T20:42:13.613

0

Red, 39 bytes

t: load s
t/month: t/month + t/day: 1
t

AnnoyingUtensil

Posted 2019-12-13T10:24:28.483

Reputation: 11

0

Retina 0.8.2, 33 bytes

T`9d`d`.9*(-12)?-..$
-23|-..$
-01

Try it online! Link includes test cases. Explanation:

T`9d`d`

Increment with wrap-around...

.9*(-12)?-..$

... the last non-9 digit, plus trailing 9s, in either the year or the month, depending on whether the month is 12. (Digits in the following date parts also get incremented.)

-23|-..$
-01

Replace the day with 01. Also, if the month was 12 before, it will be 23 now, and also needs to be reset to 01.

Neil

Posted 2019-12-13T10:24:28.483

Reputation: 95 035