Get current date fully in numbers (instead of letter) e.g. 2020-09-15

Hi,
I would like to retrieve the current date. I know there is the Time and Date command.

However I would like to write the date in the form:
YYYY-MM-DD
e.g.
2020-01-20
2020-09-15
instead of fylly written; Tuesday, September 15th 2020…

Does anyone have a suggestion how I could do this?

2020-09-15 Get date with only numberss.gh (6.7 KB)

Thank you in advance,

You’ll find useful formatting info for date and time here:

Hi!

Thank you for your answer!
I think this is exactly what I want.

Unfortunately I am not too familiar with C#.

Could you help me a bit, what part to add in the python component?

Thank you in advance :slight_smile:

2020-09-15 Get date with only numberss.gh (5.9 KB)

Here’s a Python thing; every time you push the button (or otherwise trigger the ‘x’ input?), the date/time updates:

import datetime
now = datetime.datetime.now()
Y = now.year
M = now.month
D = now.day
h = now.hour
m = now.minute
s = now.second


time_date_2019Jul8a.gh (5.5 KB)

You can adapt it to get your desired format. I left out the leading zero, though it’s not that difficult…

time_date_2020Sep15a
time_date_2020Sep15a.gh (5.3 KB)

Here is a hasty and limited version of a padLeft cluster:


time_date_2020Sep15b2
time_date_2020Sep15b.gh (9.3 KB)

Try this in Python

from datetime import date

today = date.today()

a = today.strftime("%Y-%m-%d")

or this

from datetime import datetime

today = datetime.now()
 
a = today.strftime("%Y-%m-%d")

Oh, you want date and time?

The ‘F’ (Expression) is as follows:

Format("{0}-{1}-{2} {3}:{4}:{5}", Y, M, D, h, mm, s)

time_date_2020Sep15c
time_date_2020Sep15c.gh (12.6 KB)

(re-posted because Eval input names are not case-sensitive, causing confusion between ‘M’ and ‘m’)

Why not just use the standard gh component ‘Format’?


To be found under the Set>Text menu:

6 Likes

Because we didn’t know how, thanks for showing us! Are there rules somewhere for how to use it?

That format string is tricky: {0:yyyy-MM-dd HH:mm:ss}

Can it be triggered by a button (or any code event) instead of the magic word “now”?

Here is a simpler version of Python that is pretty close but includes fraction of a second, which can easily be ignored.

import datetime
now = datetime.datetime.now()


time_date_2020Sep15d.gh (6.4 KB)

Nice trick

I suppose you should be able to use all the formatting rules given by Nathan’s link.

You may also want to have a look here for general number formatting (currencies, scientific notation etc.):

I’m not sure to understand what you mean by this:

You can construct all the date and time you want by using Construct Date and Time of the ‘Math’ panel.
image

The format component is really powerful! :slight_smile:
You can really quickly assemble inputs and generate precisely formatted output strings.
You can also make it culture sensitive (note the differences: $/€ for currency and ‘.’/’,’ as decimal separator):

In the code I posted, the date/time is updated each time the button is pressed. The same will happen with any component output replacing the button, so for example, when a lengthy process completes the date/time can be recorded.

This is different than the standard Construct Date and Construct Time components, and your “now” example, which are static values based on when the file is opened or the components are placed on the canvas. They don’t change after that.

I noticed that the ‘C’ (Culture) input to Format can be changed by right-clicking it and choosing from various options.

The ‘now’ panel coupled to the Time object does not seem to be static. Every time the solution is recomputed (by hitting F5) or re-opened, the output value gets updated.

One kind of dirty workaround that can be used to mimic an update with a button, could be to use a Pick’n’Choose component and plug the ‘now’ into both inputs. Each time the button is pressed, the output gets refreshed:

I see what you’re saying but… The purple group below is an arbitrary block of “slow code”. Changing the Value List (blue group) or the slider will trigger a “slow” re-compute that updates the time in the white group but has no effect on the “now” time in the cyan group. That’s what I mean by static.


time_date_2020Sep15e.gh (17.2 KB)