cancel
Showing results for 
Search instead for 
Did you mean: 

How to get "Day of the Year"?

Abhijit
New Contributor

I need to add “Day of the Year” as part of an output file-name. How can I do that?

5 REPLIES 5

robin
Former Employee

@Abhijit, I believe I was able to do this using the following expression:

Math.ceil((($today - $jan1st) / 1000 / 60 / 60 / 24) + ((($jan1st.getTimezoneOffset() - $today.getTimezoneOffset())) / (60 * 24)))

For clarity, I’ve split it into to two Mappers using the Expression Language. The first creates Date objects for today and for January 1st. I then use these two variables in the following expression to calculate the day of the year.

It might be easier to see the formula in this format:

Math.ceil(
(
	($today - $jan1st) / 1000 / 60 / 60 / 24
) + (
	($jan1st.getTimezoneOffset() - $today.getTimezoneOffset()) / (60 * 24)
)

)

Pipeline: 20170421_day-of-year_2017_04_21.slp (3.5 KB)

95b9f4abd1f36786ff52f4c91575868536c374e5.png

I’m getting a current “day-of-year” value of 111, which matches the result from Epoch Converter:

295981c5244265c0b945514c65957fed601ca49d.png

Alternatively, this could all be done in a single expression:

Math.ceil(((Date.now() - Date.parse(Date.now().getFullYear() + "/1/1")) / 1000 / 60 / 60 / 24) + (((Date.parse(Date.now().getFullYear() + "/1/1").getTimezoneOffset() - Date.now().getTimezoneOffset())) / (60 * 24)))

cstewart
Former Employee

In the file name field you can use an expression which looks something like:

"myfile" + Date.now().toLocaleDateTimeString('{"format":"DDD"}') + '.json'

robin
Former Employee

Ah, @cstewart that’s much easier.

Actually, the following is more efficient:

"myfile" + Date.now().toLocaleDateTimeString({"format":"DDD"}) + '.json'

(Single quotes removed around the format…)