Forum Discussion

Abhijit's avatar
Abhijit
New Contributor
9 years ago

How to get "Day of the Year"?

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

5 Replies

  • cstewart's avatar
    cstewart
    Former Employee

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

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

    • cstewart's avatar
      cstewart
      Former Employee

      Actually, the following is more efficient:

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

      (Single quotes removed around the format…)

  • robin's avatar
    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)

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

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