The CreatorCon Call for Content is officially open! Get started here.

How to set date field to 1st of next month or 1st of current month

kartikey
Tera Contributor

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

1 ACCEPTED SOLUTION

DYCM
Mega Sage

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

 

View solution in original post

4 REPLIES 4

DYCM
Mega Sage

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

 

kartikey
Tera Contributor

Yup! and what should i do for 1st of the next month? addMonths(1) ??

Correct, I updated my reply, please check the updated one. 

kartikey
Tera Contributor

Thanks a ton! but it's returning value in YYYY-MM-DD , how do i convert that in DD-MM-YYYY ??