- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-26-2024 02:01 AM
Hi Everyone,
We have a requirement to set a date field to 1st of current month and 1st of next month based on certain conditions.
I cannot use gs.beginningOfThisMonth() and gs.beginningOfNextMonth() because our instance runs on BST and these functions return values in GMT.
So suppose today it 26/06/2024 , it should return 01/07/2024 or 01/06/2024.
Could you please suggest me how should i code this?
Thanks,
kartik
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-26-2024 02:18 AM - edited ‎06-26-2024 02:21 AM
Hi @kartikey
Is this you are looking for?
Get the first day of current month
// Get the current date
var currentDate = new GlideDateTime();
// Set the day of the month to 1
currentDate.setDayOfMonth(1);
// Get the date part only (without time)
var firstDayOfMonth = currentDate.getLocalDate();
gs.info('The first day of the current month is: ' + firstDayOfMonth);
Get the first day of next month
// Get the current date
var currentDate = new GlideDateTime();
// Add one month to the current date
currentDate.addMonthsUTC(1);
// Set the day to the first day of the month
currentDate.setDayOfMonthUTC(1);
// Get the resulting date in string format
var firstDayNextMonth = currentDate.getLocalDate().toString();
gs.log("The first day of the next month is: " + firstDayNextMonth);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-26-2024 02:18 AM - edited ‎06-26-2024 02:21 AM
Hi @kartikey
Is this you are looking for?
Get the first day of current month
// Get the current date
var currentDate = new GlideDateTime();
// Set the day of the month to 1
currentDate.setDayOfMonth(1);
// Get the date part only (without time)
var firstDayOfMonth = currentDate.getLocalDate();
gs.info('The first day of the current month is: ' + firstDayOfMonth);
Get the first day of next month
// Get the current date
var currentDate = new GlideDateTime();
// Add one month to the current date
currentDate.addMonthsUTC(1);
// Set the day to the first day of the month
currentDate.setDayOfMonthUTC(1);
// Get the resulting date in string format
var firstDayNextMonth = currentDate.getLocalDate().toString();
gs.log("The first day of the next month is: " + firstDayNextMonth);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-26-2024 02:21 AM
Yup! and what should i do for 1st of the next month? addMonths(1) ??
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-26-2024 02:26 AM - edited ‎06-26-2024 02:27 AM
Correct, I updated my reply, please check the updated one.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-26-2024 02:28 AM
Thanks a ton! but it's returning value in YYYY-MM-DD , how do i convert that in DD-MM-YYYY ??