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)

1 Like

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:

10 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

1 Like

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:

1 Like

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)

Trying to work out a way to add days, hours, mins, seconds, years onto ‘system.time’

I cant get past tomorrow, today, yesterday:

Adapted from earlier post:


time_date_2022Oct24a.gh (13.5 KB)

Added ‘Year±’ slider (blue group).

Hey @Joseph_Oster

You’re skills are getting so high mate, and you are such a great help to so many people out there. thanks.

Is there difficulty in adding days / hours / minutes / seconds, due to the base 60 Sexagesimal time of the minutes and seconds, base 24 of hours, and variance of base calendar days - 30,31, leap years and so forth?

I tried looking into the system.datetime and system.datetimeoffset:

@nathanletwory

Had a go in python myself, but could not find it in the grasshopper kernel – (my python abilities are very beginner).

Cheers, Ark

Computer languages have been dealing with date/time and interval issues for decades. Languages that I’m familiar with like Java, Javascript, PHP (and to some extent Python) have similar standard methods. I suggest you do some more reading:

https://www.google.com/search?q=python+datetime

Python is pretty easy. Double-click the Python component in what I posted above, then explore what the datetime class has to offer. No harm in experimenting.

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

I was lazy to watch the time, so I made this script that captures the moment 22/2/2022 @ 22:22:22.


TIME CAPTURE.gh (13.6 KB)

2 Likes

cheers mate