How to calculate the day of the year

JPSS
Tera Contributor

How to calculate the day of the year

 

 

like 

 

 

Today Tuesday October 24, 2023 is ...
Day 297

 

10 REPLIES 10

ArsalanChaudhry
Tera Guru

Hi @JPSS,

 

// Create a GlideDate object for the specified date
var inputDate = new GlideDate('2023-10-24');

// Create a GlideDate object for January 1st of the same year
var januaryFirst = new GlideDate(inputDate.getYear() + '-01-01');

// Calculate the difference in days between the input date and January 1st
var dayOfYear = inputDate.subtract(januaryFirst);

// Add 1 to account for January 1st itself
dayOfYear += 1;

// Display the result
gs.info('Today ' + inputDate + ' is ...');
gs.info('Day ' + dayOfYear);

 

If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.

 

Thanks,
Arsalan

its showing the below year

 

 

JPSS_0-1698151000747.png

 

Martin Ivanov
Giga Sage
Giga Sage

Hello,

 

I don't think there is a Glide method for that, but you can achieve it with the JavaScript native Date object. Look at the code below:

 

var today = new Date();

var dayOfYear = Math.floor((today - new Date(today.getFullYear(), 0, 0)) / 1000 / 60 / 60 / 24);

gs.info(dayOfYear);


Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Martin Ivanov
ServiceNow MVP 2023, 2024

with Glide method is it possible

Late to the party, but in case anyone else is looking for an answer on this.  I got this to work:  

 

var gdt = new GlideDateTime();
var startOfYear = new GlideDateTime(gdt.getYear() + "-01-01 00:00:00");
var dayOfYear = gdt.getNumericValue() - startOfYear.getNumericValue();
dayOfYear = Math.ceil(dayOfYear / (1000 * 60 * 60 * 24));
gs.info('Today is day ' + dayOfYear + ' of the year.');