How to calculate the day of the year
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-23-2023 04:13 PM
How to calculate the day of the year
like
Today Tuesday October 24, 2023 is ...
Day 297
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-23-2023 11:34 PM - edited 10-23-2023 11:36 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-24-2023 05:36 AM
its showing the below year
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-23-2023 11:37 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-24-2023 05:37 AM
with Glide method is it possible
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-10-2025 08:59 AM
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.');
