DateTime.addMonths skips a month (from feb to mar)Why is the inconsistency with Close Month?Getting the year of the monthPrevious Month FormulaGetting Custom Fiscal Month and Year from Calendar DateCleaner way to get last day of previous month?How to get last tuesday of particular monthHow to get the last monday of every monthHow to retrieve start date and end date for fiscal month?How to output following month?Get previous month date to string no day

What exactly "triggers an additional time" in the interaction between Afterlife and Teysa Karlov?

What do you call the action of "describing events as they happen" like sports anchors do?

Course development: can I pay someone to make slides for the course?

How to befriend someone who doesn't like to talk?

Selecting by attribute using Python and a list

ASCII Meme Arrow Generator

Does it make sense to use a wavelet that is equal to a sine of one period?

Why do (or did, until very recently) aircraft transponders wait to be interrogated before broadcasting beacon signals?

Is it advisable to add a location heads-up when a scene changes in a novel?

A life of PhD: is it feasible?

How to make a composition of functions prettier?

How can powerful telekinesis avoid violating Newton's 3rd Law?

Grandpa has another non math question

Is it true that "only photographers care about noise"?

How to handle when PCs taste a potion that is actually poison?

Can a Warforged suffer from magical exhaustion?

Why would a home insurer offer a discount based on credit score?

How to avoid typing 'git' at the begining of every Git command

post-head emotive modifiers such as "in the world" and "the hell"

Why is it bad to use your whole foot in rock climbing

What's the best way to quit a job mostly because of money?

How to Handle Many Times Series Simultaneously?

How does AFV select the winning videos?

Entered UK using my now-lost UK passport; can I go to Spain using my US passport?



DateTime.addMonths skips a month (from feb to mar)


Why is the inconsistency with Close Month?Getting the year of the monthPrevious Month FormulaGetting Custom Fiscal Month and Year from Calendar DateCleaner way to get last day of previous month?How to get last tuesday of particular monthHow to get the last monday of every monthHow to retrieve start date and end date for fiscal month?How to output following month?Get previous month date to string no day






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








5















I'm writing some code to get each months name between a start and an end date. This works fine for most months - except February into march, and november - december.



Heres the code :



Date startDate = Date.parse('5/30/2019');
Date endDate = startDate.addYears(1);

Integer durationInMonths = StartDate.MonthsBetween(EndDate);

for (Integer i = 0; i <= durationInMonths; i++)
DateTime m = DateTime.newInstance( StartDate.Year(), StartDate.Month(), 1);

m = m.addMonths(i);

System.debug(i);
System.debug(m.Day() + ' / ' + m.Month() + ' / ' + String.valueOf(m.year()));



Here you can see the crazy output (DD/MM/YY) - at one point (1/11/19 -> 30/11/19) the date picks up some days instead of months and then the whole thing falls apart. The scary part is that I didn't notice until now due to the output being put into a set and only using (MM/YY).



enter image description here










share|improve this question



















  • 3





    If you notice, blunder happen when Daylight savings is enabled or disabled in march and November.

    – Pranay Jaiswal
    7 hours ago






  • 1





    Try to change your DateTime into a Date and this likely goes away.

    – Sebastian Kessel
    7 hours ago

















5















I'm writing some code to get each months name between a start and an end date. This works fine for most months - except February into march, and november - december.



Heres the code :



Date startDate = Date.parse('5/30/2019');
Date endDate = startDate.addYears(1);

Integer durationInMonths = StartDate.MonthsBetween(EndDate);

for (Integer i = 0; i <= durationInMonths; i++)
DateTime m = DateTime.newInstance( StartDate.Year(), StartDate.Month(), 1);

m = m.addMonths(i);

System.debug(i);
System.debug(m.Day() + ' / ' + m.Month() + ' / ' + String.valueOf(m.year()));



Here you can see the crazy output (DD/MM/YY) - at one point (1/11/19 -> 30/11/19) the date picks up some days instead of months and then the whole thing falls apart. The scary part is that I didn't notice until now due to the output being put into a set and only using (MM/YY).



enter image description here










share|improve this question



















  • 3





    If you notice, blunder happen when Daylight savings is enabled or disabled in march and November.

    – Pranay Jaiswal
    7 hours ago






  • 1





    Try to change your DateTime into a Date and this likely goes away.

    – Sebastian Kessel
    7 hours ago













5












5








5








I'm writing some code to get each months name between a start and an end date. This works fine for most months - except February into march, and november - december.



Heres the code :



Date startDate = Date.parse('5/30/2019');
Date endDate = startDate.addYears(1);

Integer durationInMonths = StartDate.MonthsBetween(EndDate);

for (Integer i = 0; i <= durationInMonths; i++)
DateTime m = DateTime.newInstance( StartDate.Year(), StartDate.Month(), 1);

m = m.addMonths(i);

System.debug(i);
System.debug(m.Day() + ' / ' + m.Month() + ' / ' + String.valueOf(m.year()));



Here you can see the crazy output (DD/MM/YY) - at one point (1/11/19 -> 30/11/19) the date picks up some days instead of months and then the whole thing falls apart. The scary part is that I didn't notice until now due to the output being put into a set and only using (MM/YY).



enter image description here










share|improve this question
















I'm writing some code to get each months name between a start and an end date. This works fine for most months - except February into march, and november - december.



Heres the code :



Date startDate = Date.parse('5/30/2019');
Date endDate = startDate.addYears(1);

Integer durationInMonths = StartDate.MonthsBetween(EndDate);

for (Integer i = 0; i <= durationInMonths; i++)
DateTime m = DateTime.newInstance( StartDate.Year(), StartDate.Month(), 1);

m = m.addMonths(i);

System.debug(i);
System.debug(m.Day() + ' / ' + m.Month() + ' / ' + String.valueOf(m.year()));



Here you can see the crazy output (DD/MM/YY) - at one point (1/11/19 -> 30/11/19) the date picks up some days instead of months and then the whole thing falls apart. The scary part is that I didn't notice until now due to the output being put into a set and only using (MM/YY).



enter image description here







apex date






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 7 hours ago







battery.cord

















asked 8 hours ago









battery.cordbattery.cord

7,29551848




7,29551848







  • 3





    If you notice, blunder happen when Daylight savings is enabled or disabled in march and November.

    – Pranay Jaiswal
    7 hours ago






  • 1





    Try to change your DateTime into a Date and this likely goes away.

    – Sebastian Kessel
    7 hours ago












  • 3





    If you notice, blunder happen when Daylight savings is enabled or disabled in march and November.

    – Pranay Jaiswal
    7 hours ago






  • 1





    Try to change your DateTime into a Date and this likely goes away.

    – Sebastian Kessel
    7 hours ago







3




3





If you notice, blunder happen when Daylight savings is enabled or disabled in march and November.

– Pranay Jaiswal
7 hours ago





If you notice, blunder happen when Daylight savings is enabled or disabled in march and November.

– Pranay Jaiswal
7 hours ago




1




1





Try to change your DateTime into a Date and this likely goes away.

– Sebastian Kessel
7 hours ago





Try to change your DateTime into a Date and this likely goes away.

– Sebastian Kessel
7 hours ago










2 Answers
2






active

oldest

votes


















3














Not an answer, but a clue, it seems it has to do something related to timezone and summer time. Same code in my BST org gives a different response.



Date startDate = Date.parse('30/05/2019');
Date endDate = startDate.addYears(1);

Integer durationInMonths = StartDate.MonthsBetween(EndDate);

for (Integer i = 0; i <= durationInMonths; i++)
DateTime m = DateTime.newInstance( StartDate.Year(), StartDate.Month(), 1);

m = m.addMonths(i);

System.debug(i);
System.debug(m.Day() + ' / ' + m.Month() + ' / ' + String.valueOf(m.year()));



Debug :



17:17:14.14 (16267512)|USER_DEBUG|[11]|DEBUG|0
17:17:14.14 (16351252)|USER_DEBUG|[12]|DEBUG|1 / 5 / 2019
17:17:14.14 (16404681)|USER_DEBUG|[11]|DEBUG|1
17:17:14.14 (16434289)|USER_DEBUG|[12]|DEBUG|31 / 5 / 2019
17:17:14.14 (16453217)|USER_DEBUG|[11]|DEBUG|2
17:17:14.14 (16480510)|USER_DEBUG|[12]|DEBUG|1 / 7 / 2019
17:17:14.14 (16498216)|USER_DEBUG|[11]|DEBUG|3
17:17:14.14 (16524565)|USER_DEBUG|[12]|DEBUG|31 / 7 / 2019
17:17:14.14 (16542405)|USER_DEBUG|[11]|DEBUG|4
17:17:14.14 (16568781)|USER_DEBUG|[12]|DEBUG|31 / 8 / 2019
17:17:14.14 (16592710)|USER_DEBUG|[11]|DEBUG|5
17:17:14.14 (16619134)|USER_DEBUG|[12]|DEBUG|1 / 10 / 2019
17:17:14.14 (16636368)|USER_DEBUG|[11]|DEBUG|6
17:17:14.14 (16663050)|USER_DEBUG|[12]|DEBUG|30 / 10 / 2019
17:17:14.14 (16680525)|USER_DEBUG|[11]|DEBUG|7
17:17:14.14 (16706633)|USER_DEBUG|[12]|DEBUG|30 / 11 / 2019
17:17:14.14 (16723573)|USER_DEBUG|[11]|DEBUG|8
17:17:14.14 (16750065)|USER_DEBUG|[12]|DEBUG|30 / 12 / 2019
17:17:14.14 (16767156)|USER_DEBUG|[11]|DEBUG|9
17:17:14.14 (16793676)|USER_DEBUG|[12]|DEBUG|30 / 1 / 2020
17:17:14.14 (16810795)|USER_DEBUG|[11]|DEBUG|10
17:17:14.14 (16837147)|USER_DEBUG|[12]|DEBUG|29 / 2 / 2020
17:17:14.14 (16854164)|USER_DEBUG|[11]|DEBUG|11
17:17:14.14 (16880438)|USER_DEBUG|[12]|DEBUG|31 / 3 / 2020
17:17:14.14 (16897409)|USER_DEBUG|[11]|DEBUG|12
17:17:14.14 (16923463)|USER_DEBUG|[12]|DEBUG|1 / 5 / 2020


EDIT:



When you do



Date startDate = Date.parse('30/05/2019');

System.debug(startDate); //Prints 2019-05-30 00:00:00


Which is SummerTime/DaylightSaving 1 hour ahead. So Actual DateTime is 2019-05-29 23:00:00



Am not sure if there can be more than 1 hour in DST, so on safe side I have added 2 hours as offset and it gives me correct response.



Datetime startDate = Datetime.newInstance(2019,05,30 , 2, 0, 0);


for(Integer i =0 ;i < 12;i++)
startDate = startDate.addMonths(1);
System.debug(startDate);



Debug :



17:47:17.14 (15747813)|USER_DEBUG|[6]|DEBUG|2019-06-30 01:00:00
17:47:17.14 (15781585)|USER_DEBUG|[6]|DEBUG|2019-07-30 01:00:00
17:47:17.14 (15793019)|USER_DEBUG|[6]|DEBUG|2019-08-30 01:00:00
17:47:17.14 (15804331)|USER_DEBUG|[6]|DEBUG|2019-09-30 01:00:00
17:47:17.14 (15817668)|USER_DEBUG|[6]|DEBUG|2019-10-30 01:00:00
17:47:17.14 (15827719)|USER_DEBUG|[6]|DEBUG|2019-11-30 01:00:00
17:47:17.14 (15837722)|USER_DEBUG|[6]|DEBUG|2019-12-30 01:00:00
17:47:17.14 (15848084)|USER_DEBUG|[6]|DEBUG|2020-01-30 01:00:00
17:47:17.14 (15862108)|USER_DEBUG|[6]|DEBUG|2020-02-29 01:00:00
17:47:17.14 (15874963)|USER_DEBUG|[6]|DEBUG|2020-03-29 01:00:00
17:47:17.14 (15884811)|USER_DEBUG|[6]|DEBUG|2020-04-29 01:00:00
17:47:17.14 (15894518)|USER_DEBUG|[6]|DEBUG|2020-05-29 01:00:00





share|improve this answer




















  • 1





    I think you're onto something.... because the OP's dates come back to normal in April (DST ends in March in America) and yours come back to normal in May (DST ends in April in Europe). I think what is happening is that the system is subtracting/adding the extra hour and displaying the time accordingly. Bizarre

    – Sebastian Kessel
    7 hours ago











  • Thats definitely whats going on - makes for some very confusing output!

    – battery.cord
    7 hours ago


















2














Easy solution that I want to post here - don't use addMonths. It looks like it just adds 30 days to the date, which isn't a valid solution in the slightest and produces problems like above. My mistake was assuming that addMonths wouldn't modify the day part of the date, which it totally will!



Instead, just modify the month given to the constructor.



Date startDate = Date.parse('5/30/2019');
Date endDate = startDate.addYears(1);

Integer durationInMonths = StartDate.MonthsBetween(EndDate);

for (Integer i = 0; i <= durationInMonths; i++)
DateTime m = DateTime.newInstance( StartDate.Year(), StartDate.Month() + i, 1);

System.debug(i);
System.debug(m.Day() + ' / ' + m.Month() + ' / ' + String.valueOf(m.year()));



This will output a date with the first day and the given month.



enter image description here






Other solution (@Sebastian Kessel) - just use a Date instead of DateTime:



Date startDate = Date.parse('5/30/2019');
Date endDate = startDate.addYears(1);

Integer durationInMonths = StartDate.MonthsBetween(EndDate);

for (Integer i = 0; i <= durationInMonths; i++)
Date m = Date.newInstance( StartDate.Year(), StartDate.Month(), 1);

m = m.addMonths(i);






share|improve this answer

























    Your Answer








    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "459"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f265350%2fdatetime-addmonths-skips-a-month-from-feb-to-mar%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    Not an answer, but a clue, it seems it has to do something related to timezone and summer time. Same code in my BST org gives a different response.



    Date startDate = Date.parse('30/05/2019');
    Date endDate = startDate.addYears(1);

    Integer durationInMonths = StartDate.MonthsBetween(EndDate);

    for (Integer i = 0; i <= durationInMonths; i++)
    DateTime m = DateTime.newInstance( StartDate.Year(), StartDate.Month(), 1);

    m = m.addMonths(i);

    System.debug(i);
    System.debug(m.Day() + ' / ' + m.Month() + ' / ' + String.valueOf(m.year()));



    Debug :



    17:17:14.14 (16267512)|USER_DEBUG|[11]|DEBUG|0
    17:17:14.14 (16351252)|USER_DEBUG|[12]|DEBUG|1 / 5 / 2019
    17:17:14.14 (16404681)|USER_DEBUG|[11]|DEBUG|1
    17:17:14.14 (16434289)|USER_DEBUG|[12]|DEBUG|31 / 5 / 2019
    17:17:14.14 (16453217)|USER_DEBUG|[11]|DEBUG|2
    17:17:14.14 (16480510)|USER_DEBUG|[12]|DEBUG|1 / 7 / 2019
    17:17:14.14 (16498216)|USER_DEBUG|[11]|DEBUG|3
    17:17:14.14 (16524565)|USER_DEBUG|[12]|DEBUG|31 / 7 / 2019
    17:17:14.14 (16542405)|USER_DEBUG|[11]|DEBUG|4
    17:17:14.14 (16568781)|USER_DEBUG|[12]|DEBUG|31 / 8 / 2019
    17:17:14.14 (16592710)|USER_DEBUG|[11]|DEBUG|5
    17:17:14.14 (16619134)|USER_DEBUG|[12]|DEBUG|1 / 10 / 2019
    17:17:14.14 (16636368)|USER_DEBUG|[11]|DEBUG|6
    17:17:14.14 (16663050)|USER_DEBUG|[12]|DEBUG|30 / 10 / 2019
    17:17:14.14 (16680525)|USER_DEBUG|[11]|DEBUG|7
    17:17:14.14 (16706633)|USER_DEBUG|[12]|DEBUG|30 / 11 / 2019
    17:17:14.14 (16723573)|USER_DEBUG|[11]|DEBUG|8
    17:17:14.14 (16750065)|USER_DEBUG|[12]|DEBUG|30 / 12 / 2019
    17:17:14.14 (16767156)|USER_DEBUG|[11]|DEBUG|9
    17:17:14.14 (16793676)|USER_DEBUG|[12]|DEBUG|30 / 1 / 2020
    17:17:14.14 (16810795)|USER_DEBUG|[11]|DEBUG|10
    17:17:14.14 (16837147)|USER_DEBUG|[12]|DEBUG|29 / 2 / 2020
    17:17:14.14 (16854164)|USER_DEBUG|[11]|DEBUG|11
    17:17:14.14 (16880438)|USER_DEBUG|[12]|DEBUG|31 / 3 / 2020
    17:17:14.14 (16897409)|USER_DEBUG|[11]|DEBUG|12
    17:17:14.14 (16923463)|USER_DEBUG|[12]|DEBUG|1 / 5 / 2020


    EDIT:



    When you do



    Date startDate = Date.parse('30/05/2019');

    System.debug(startDate); //Prints 2019-05-30 00:00:00


    Which is SummerTime/DaylightSaving 1 hour ahead. So Actual DateTime is 2019-05-29 23:00:00



    Am not sure if there can be more than 1 hour in DST, so on safe side I have added 2 hours as offset and it gives me correct response.



    Datetime startDate = Datetime.newInstance(2019,05,30 , 2, 0, 0);


    for(Integer i =0 ;i < 12;i++)
    startDate = startDate.addMonths(1);
    System.debug(startDate);



    Debug :



    17:47:17.14 (15747813)|USER_DEBUG|[6]|DEBUG|2019-06-30 01:00:00
    17:47:17.14 (15781585)|USER_DEBUG|[6]|DEBUG|2019-07-30 01:00:00
    17:47:17.14 (15793019)|USER_DEBUG|[6]|DEBUG|2019-08-30 01:00:00
    17:47:17.14 (15804331)|USER_DEBUG|[6]|DEBUG|2019-09-30 01:00:00
    17:47:17.14 (15817668)|USER_DEBUG|[6]|DEBUG|2019-10-30 01:00:00
    17:47:17.14 (15827719)|USER_DEBUG|[6]|DEBUG|2019-11-30 01:00:00
    17:47:17.14 (15837722)|USER_DEBUG|[6]|DEBUG|2019-12-30 01:00:00
    17:47:17.14 (15848084)|USER_DEBUG|[6]|DEBUG|2020-01-30 01:00:00
    17:47:17.14 (15862108)|USER_DEBUG|[6]|DEBUG|2020-02-29 01:00:00
    17:47:17.14 (15874963)|USER_DEBUG|[6]|DEBUG|2020-03-29 01:00:00
    17:47:17.14 (15884811)|USER_DEBUG|[6]|DEBUG|2020-04-29 01:00:00
    17:47:17.14 (15894518)|USER_DEBUG|[6]|DEBUG|2020-05-29 01:00:00





    share|improve this answer




















    • 1





      I think you're onto something.... because the OP's dates come back to normal in April (DST ends in March in America) and yours come back to normal in May (DST ends in April in Europe). I think what is happening is that the system is subtracting/adding the extra hour and displaying the time accordingly. Bizarre

      – Sebastian Kessel
      7 hours ago











    • Thats definitely whats going on - makes for some very confusing output!

      – battery.cord
      7 hours ago















    3














    Not an answer, but a clue, it seems it has to do something related to timezone and summer time. Same code in my BST org gives a different response.



    Date startDate = Date.parse('30/05/2019');
    Date endDate = startDate.addYears(1);

    Integer durationInMonths = StartDate.MonthsBetween(EndDate);

    for (Integer i = 0; i <= durationInMonths; i++)
    DateTime m = DateTime.newInstance( StartDate.Year(), StartDate.Month(), 1);

    m = m.addMonths(i);

    System.debug(i);
    System.debug(m.Day() + ' / ' + m.Month() + ' / ' + String.valueOf(m.year()));



    Debug :



    17:17:14.14 (16267512)|USER_DEBUG|[11]|DEBUG|0
    17:17:14.14 (16351252)|USER_DEBUG|[12]|DEBUG|1 / 5 / 2019
    17:17:14.14 (16404681)|USER_DEBUG|[11]|DEBUG|1
    17:17:14.14 (16434289)|USER_DEBUG|[12]|DEBUG|31 / 5 / 2019
    17:17:14.14 (16453217)|USER_DEBUG|[11]|DEBUG|2
    17:17:14.14 (16480510)|USER_DEBUG|[12]|DEBUG|1 / 7 / 2019
    17:17:14.14 (16498216)|USER_DEBUG|[11]|DEBUG|3
    17:17:14.14 (16524565)|USER_DEBUG|[12]|DEBUG|31 / 7 / 2019
    17:17:14.14 (16542405)|USER_DEBUG|[11]|DEBUG|4
    17:17:14.14 (16568781)|USER_DEBUG|[12]|DEBUG|31 / 8 / 2019
    17:17:14.14 (16592710)|USER_DEBUG|[11]|DEBUG|5
    17:17:14.14 (16619134)|USER_DEBUG|[12]|DEBUG|1 / 10 / 2019
    17:17:14.14 (16636368)|USER_DEBUG|[11]|DEBUG|6
    17:17:14.14 (16663050)|USER_DEBUG|[12]|DEBUG|30 / 10 / 2019
    17:17:14.14 (16680525)|USER_DEBUG|[11]|DEBUG|7
    17:17:14.14 (16706633)|USER_DEBUG|[12]|DEBUG|30 / 11 / 2019
    17:17:14.14 (16723573)|USER_DEBUG|[11]|DEBUG|8
    17:17:14.14 (16750065)|USER_DEBUG|[12]|DEBUG|30 / 12 / 2019
    17:17:14.14 (16767156)|USER_DEBUG|[11]|DEBUG|9
    17:17:14.14 (16793676)|USER_DEBUG|[12]|DEBUG|30 / 1 / 2020
    17:17:14.14 (16810795)|USER_DEBUG|[11]|DEBUG|10
    17:17:14.14 (16837147)|USER_DEBUG|[12]|DEBUG|29 / 2 / 2020
    17:17:14.14 (16854164)|USER_DEBUG|[11]|DEBUG|11
    17:17:14.14 (16880438)|USER_DEBUG|[12]|DEBUG|31 / 3 / 2020
    17:17:14.14 (16897409)|USER_DEBUG|[11]|DEBUG|12
    17:17:14.14 (16923463)|USER_DEBUG|[12]|DEBUG|1 / 5 / 2020


    EDIT:



    When you do



    Date startDate = Date.parse('30/05/2019');

    System.debug(startDate); //Prints 2019-05-30 00:00:00


    Which is SummerTime/DaylightSaving 1 hour ahead. So Actual DateTime is 2019-05-29 23:00:00



    Am not sure if there can be more than 1 hour in DST, so on safe side I have added 2 hours as offset and it gives me correct response.



    Datetime startDate = Datetime.newInstance(2019,05,30 , 2, 0, 0);


    for(Integer i =0 ;i < 12;i++)
    startDate = startDate.addMonths(1);
    System.debug(startDate);



    Debug :



    17:47:17.14 (15747813)|USER_DEBUG|[6]|DEBUG|2019-06-30 01:00:00
    17:47:17.14 (15781585)|USER_DEBUG|[6]|DEBUG|2019-07-30 01:00:00
    17:47:17.14 (15793019)|USER_DEBUG|[6]|DEBUG|2019-08-30 01:00:00
    17:47:17.14 (15804331)|USER_DEBUG|[6]|DEBUG|2019-09-30 01:00:00
    17:47:17.14 (15817668)|USER_DEBUG|[6]|DEBUG|2019-10-30 01:00:00
    17:47:17.14 (15827719)|USER_DEBUG|[6]|DEBUG|2019-11-30 01:00:00
    17:47:17.14 (15837722)|USER_DEBUG|[6]|DEBUG|2019-12-30 01:00:00
    17:47:17.14 (15848084)|USER_DEBUG|[6]|DEBUG|2020-01-30 01:00:00
    17:47:17.14 (15862108)|USER_DEBUG|[6]|DEBUG|2020-02-29 01:00:00
    17:47:17.14 (15874963)|USER_DEBUG|[6]|DEBUG|2020-03-29 01:00:00
    17:47:17.14 (15884811)|USER_DEBUG|[6]|DEBUG|2020-04-29 01:00:00
    17:47:17.14 (15894518)|USER_DEBUG|[6]|DEBUG|2020-05-29 01:00:00





    share|improve this answer




















    • 1





      I think you're onto something.... because the OP's dates come back to normal in April (DST ends in March in America) and yours come back to normal in May (DST ends in April in Europe). I think what is happening is that the system is subtracting/adding the extra hour and displaying the time accordingly. Bizarre

      – Sebastian Kessel
      7 hours ago











    • Thats definitely whats going on - makes for some very confusing output!

      – battery.cord
      7 hours ago













    3












    3








    3







    Not an answer, but a clue, it seems it has to do something related to timezone and summer time. Same code in my BST org gives a different response.



    Date startDate = Date.parse('30/05/2019');
    Date endDate = startDate.addYears(1);

    Integer durationInMonths = StartDate.MonthsBetween(EndDate);

    for (Integer i = 0; i <= durationInMonths; i++)
    DateTime m = DateTime.newInstance( StartDate.Year(), StartDate.Month(), 1);

    m = m.addMonths(i);

    System.debug(i);
    System.debug(m.Day() + ' / ' + m.Month() + ' / ' + String.valueOf(m.year()));



    Debug :



    17:17:14.14 (16267512)|USER_DEBUG|[11]|DEBUG|0
    17:17:14.14 (16351252)|USER_DEBUG|[12]|DEBUG|1 / 5 / 2019
    17:17:14.14 (16404681)|USER_DEBUG|[11]|DEBUG|1
    17:17:14.14 (16434289)|USER_DEBUG|[12]|DEBUG|31 / 5 / 2019
    17:17:14.14 (16453217)|USER_DEBUG|[11]|DEBUG|2
    17:17:14.14 (16480510)|USER_DEBUG|[12]|DEBUG|1 / 7 / 2019
    17:17:14.14 (16498216)|USER_DEBUG|[11]|DEBUG|3
    17:17:14.14 (16524565)|USER_DEBUG|[12]|DEBUG|31 / 7 / 2019
    17:17:14.14 (16542405)|USER_DEBUG|[11]|DEBUG|4
    17:17:14.14 (16568781)|USER_DEBUG|[12]|DEBUG|31 / 8 / 2019
    17:17:14.14 (16592710)|USER_DEBUG|[11]|DEBUG|5
    17:17:14.14 (16619134)|USER_DEBUG|[12]|DEBUG|1 / 10 / 2019
    17:17:14.14 (16636368)|USER_DEBUG|[11]|DEBUG|6
    17:17:14.14 (16663050)|USER_DEBUG|[12]|DEBUG|30 / 10 / 2019
    17:17:14.14 (16680525)|USER_DEBUG|[11]|DEBUG|7
    17:17:14.14 (16706633)|USER_DEBUG|[12]|DEBUG|30 / 11 / 2019
    17:17:14.14 (16723573)|USER_DEBUG|[11]|DEBUG|8
    17:17:14.14 (16750065)|USER_DEBUG|[12]|DEBUG|30 / 12 / 2019
    17:17:14.14 (16767156)|USER_DEBUG|[11]|DEBUG|9
    17:17:14.14 (16793676)|USER_DEBUG|[12]|DEBUG|30 / 1 / 2020
    17:17:14.14 (16810795)|USER_DEBUG|[11]|DEBUG|10
    17:17:14.14 (16837147)|USER_DEBUG|[12]|DEBUG|29 / 2 / 2020
    17:17:14.14 (16854164)|USER_DEBUG|[11]|DEBUG|11
    17:17:14.14 (16880438)|USER_DEBUG|[12]|DEBUG|31 / 3 / 2020
    17:17:14.14 (16897409)|USER_DEBUG|[11]|DEBUG|12
    17:17:14.14 (16923463)|USER_DEBUG|[12]|DEBUG|1 / 5 / 2020


    EDIT:



    When you do



    Date startDate = Date.parse('30/05/2019');

    System.debug(startDate); //Prints 2019-05-30 00:00:00


    Which is SummerTime/DaylightSaving 1 hour ahead. So Actual DateTime is 2019-05-29 23:00:00



    Am not sure if there can be more than 1 hour in DST, so on safe side I have added 2 hours as offset and it gives me correct response.



    Datetime startDate = Datetime.newInstance(2019,05,30 , 2, 0, 0);


    for(Integer i =0 ;i < 12;i++)
    startDate = startDate.addMonths(1);
    System.debug(startDate);



    Debug :



    17:47:17.14 (15747813)|USER_DEBUG|[6]|DEBUG|2019-06-30 01:00:00
    17:47:17.14 (15781585)|USER_DEBUG|[6]|DEBUG|2019-07-30 01:00:00
    17:47:17.14 (15793019)|USER_DEBUG|[6]|DEBUG|2019-08-30 01:00:00
    17:47:17.14 (15804331)|USER_DEBUG|[6]|DEBUG|2019-09-30 01:00:00
    17:47:17.14 (15817668)|USER_DEBUG|[6]|DEBUG|2019-10-30 01:00:00
    17:47:17.14 (15827719)|USER_DEBUG|[6]|DEBUG|2019-11-30 01:00:00
    17:47:17.14 (15837722)|USER_DEBUG|[6]|DEBUG|2019-12-30 01:00:00
    17:47:17.14 (15848084)|USER_DEBUG|[6]|DEBUG|2020-01-30 01:00:00
    17:47:17.14 (15862108)|USER_DEBUG|[6]|DEBUG|2020-02-29 01:00:00
    17:47:17.14 (15874963)|USER_DEBUG|[6]|DEBUG|2020-03-29 01:00:00
    17:47:17.14 (15884811)|USER_DEBUG|[6]|DEBUG|2020-04-29 01:00:00
    17:47:17.14 (15894518)|USER_DEBUG|[6]|DEBUG|2020-05-29 01:00:00





    share|improve this answer















    Not an answer, but a clue, it seems it has to do something related to timezone and summer time. Same code in my BST org gives a different response.



    Date startDate = Date.parse('30/05/2019');
    Date endDate = startDate.addYears(1);

    Integer durationInMonths = StartDate.MonthsBetween(EndDate);

    for (Integer i = 0; i <= durationInMonths; i++)
    DateTime m = DateTime.newInstance( StartDate.Year(), StartDate.Month(), 1);

    m = m.addMonths(i);

    System.debug(i);
    System.debug(m.Day() + ' / ' + m.Month() + ' / ' + String.valueOf(m.year()));



    Debug :



    17:17:14.14 (16267512)|USER_DEBUG|[11]|DEBUG|0
    17:17:14.14 (16351252)|USER_DEBUG|[12]|DEBUG|1 / 5 / 2019
    17:17:14.14 (16404681)|USER_DEBUG|[11]|DEBUG|1
    17:17:14.14 (16434289)|USER_DEBUG|[12]|DEBUG|31 / 5 / 2019
    17:17:14.14 (16453217)|USER_DEBUG|[11]|DEBUG|2
    17:17:14.14 (16480510)|USER_DEBUG|[12]|DEBUG|1 / 7 / 2019
    17:17:14.14 (16498216)|USER_DEBUG|[11]|DEBUG|3
    17:17:14.14 (16524565)|USER_DEBUG|[12]|DEBUG|31 / 7 / 2019
    17:17:14.14 (16542405)|USER_DEBUG|[11]|DEBUG|4
    17:17:14.14 (16568781)|USER_DEBUG|[12]|DEBUG|31 / 8 / 2019
    17:17:14.14 (16592710)|USER_DEBUG|[11]|DEBUG|5
    17:17:14.14 (16619134)|USER_DEBUG|[12]|DEBUG|1 / 10 / 2019
    17:17:14.14 (16636368)|USER_DEBUG|[11]|DEBUG|6
    17:17:14.14 (16663050)|USER_DEBUG|[12]|DEBUG|30 / 10 / 2019
    17:17:14.14 (16680525)|USER_DEBUG|[11]|DEBUG|7
    17:17:14.14 (16706633)|USER_DEBUG|[12]|DEBUG|30 / 11 / 2019
    17:17:14.14 (16723573)|USER_DEBUG|[11]|DEBUG|8
    17:17:14.14 (16750065)|USER_DEBUG|[12]|DEBUG|30 / 12 / 2019
    17:17:14.14 (16767156)|USER_DEBUG|[11]|DEBUG|9
    17:17:14.14 (16793676)|USER_DEBUG|[12]|DEBUG|30 / 1 / 2020
    17:17:14.14 (16810795)|USER_DEBUG|[11]|DEBUG|10
    17:17:14.14 (16837147)|USER_DEBUG|[12]|DEBUG|29 / 2 / 2020
    17:17:14.14 (16854164)|USER_DEBUG|[11]|DEBUG|11
    17:17:14.14 (16880438)|USER_DEBUG|[12]|DEBUG|31 / 3 / 2020
    17:17:14.14 (16897409)|USER_DEBUG|[11]|DEBUG|12
    17:17:14.14 (16923463)|USER_DEBUG|[12]|DEBUG|1 / 5 / 2020


    EDIT:



    When you do



    Date startDate = Date.parse('30/05/2019');

    System.debug(startDate); //Prints 2019-05-30 00:00:00


    Which is SummerTime/DaylightSaving 1 hour ahead. So Actual DateTime is 2019-05-29 23:00:00



    Am not sure if there can be more than 1 hour in DST, so on safe side I have added 2 hours as offset and it gives me correct response.



    Datetime startDate = Datetime.newInstance(2019,05,30 , 2, 0, 0);


    for(Integer i =0 ;i < 12;i++)
    startDate = startDate.addMonths(1);
    System.debug(startDate);



    Debug :



    17:47:17.14 (15747813)|USER_DEBUG|[6]|DEBUG|2019-06-30 01:00:00
    17:47:17.14 (15781585)|USER_DEBUG|[6]|DEBUG|2019-07-30 01:00:00
    17:47:17.14 (15793019)|USER_DEBUG|[6]|DEBUG|2019-08-30 01:00:00
    17:47:17.14 (15804331)|USER_DEBUG|[6]|DEBUG|2019-09-30 01:00:00
    17:47:17.14 (15817668)|USER_DEBUG|[6]|DEBUG|2019-10-30 01:00:00
    17:47:17.14 (15827719)|USER_DEBUG|[6]|DEBUG|2019-11-30 01:00:00
    17:47:17.14 (15837722)|USER_DEBUG|[6]|DEBUG|2019-12-30 01:00:00
    17:47:17.14 (15848084)|USER_DEBUG|[6]|DEBUG|2020-01-30 01:00:00
    17:47:17.14 (15862108)|USER_DEBUG|[6]|DEBUG|2020-02-29 01:00:00
    17:47:17.14 (15874963)|USER_DEBUG|[6]|DEBUG|2020-03-29 01:00:00
    17:47:17.14 (15884811)|USER_DEBUG|[6]|DEBUG|2020-04-29 01:00:00
    17:47:17.14 (15894518)|USER_DEBUG|[6]|DEBUG|2020-05-29 01:00:00






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited 7 hours ago

























    answered 8 hours ago









    Pranay JaiswalPranay Jaiswal

    20.8k53367




    20.8k53367







    • 1





      I think you're onto something.... because the OP's dates come back to normal in April (DST ends in March in America) and yours come back to normal in May (DST ends in April in Europe). I think what is happening is that the system is subtracting/adding the extra hour and displaying the time accordingly. Bizarre

      – Sebastian Kessel
      7 hours ago











    • Thats definitely whats going on - makes for some very confusing output!

      – battery.cord
      7 hours ago












    • 1





      I think you're onto something.... because the OP's dates come back to normal in April (DST ends in March in America) and yours come back to normal in May (DST ends in April in Europe). I think what is happening is that the system is subtracting/adding the extra hour and displaying the time accordingly. Bizarre

      – Sebastian Kessel
      7 hours ago











    • Thats definitely whats going on - makes for some very confusing output!

      – battery.cord
      7 hours ago







    1




    1





    I think you're onto something.... because the OP's dates come back to normal in April (DST ends in March in America) and yours come back to normal in May (DST ends in April in Europe). I think what is happening is that the system is subtracting/adding the extra hour and displaying the time accordingly. Bizarre

    – Sebastian Kessel
    7 hours ago





    I think you're onto something.... because the OP's dates come back to normal in April (DST ends in March in America) and yours come back to normal in May (DST ends in April in Europe). I think what is happening is that the system is subtracting/adding the extra hour and displaying the time accordingly. Bizarre

    – Sebastian Kessel
    7 hours ago













    Thats definitely whats going on - makes for some very confusing output!

    – battery.cord
    7 hours ago





    Thats definitely whats going on - makes for some very confusing output!

    – battery.cord
    7 hours ago













    2














    Easy solution that I want to post here - don't use addMonths. It looks like it just adds 30 days to the date, which isn't a valid solution in the slightest and produces problems like above. My mistake was assuming that addMonths wouldn't modify the day part of the date, which it totally will!



    Instead, just modify the month given to the constructor.



    Date startDate = Date.parse('5/30/2019');
    Date endDate = startDate.addYears(1);

    Integer durationInMonths = StartDate.MonthsBetween(EndDate);

    for (Integer i = 0; i <= durationInMonths; i++)
    DateTime m = DateTime.newInstance( StartDate.Year(), StartDate.Month() + i, 1);

    System.debug(i);
    System.debug(m.Day() + ' / ' + m.Month() + ' / ' + String.valueOf(m.year()));



    This will output a date with the first day and the given month.



    enter image description here






    Other solution (@Sebastian Kessel) - just use a Date instead of DateTime:



    Date startDate = Date.parse('5/30/2019');
    Date endDate = startDate.addYears(1);

    Integer durationInMonths = StartDate.MonthsBetween(EndDate);

    for (Integer i = 0; i <= durationInMonths; i++)
    Date m = Date.newInstance( StartDate.Year(), StartDate.Month(), 1);

    m = m.addMonths(i);






    share|improve this answer





























      2














      Easy solution that I want to post here - don't use addMonths. It looks like it just adds 30 days to the date, which isn't a valid solution in the slightest and produces problems like above. My mistake was assuming that addMonths wouldn't modify the day part of the date, which it totally will!



      Instead, just modify the month given to the constructor.



      Date startDate = Date.parse('5/30/2019');
      Date endDate = startDate.addYears(1);

      Integer durationInMonths = StartDate.MonthsBetween(EndDate);

      for (Integer i = 0; i <= durationInMonths; i++)
      DateTime m = DateTime.newInstance( StartDate.Year(), StartDate.Month() + i, 1);

      System.debug(i);
      System.debug(m.Day() + ' / ' + m.Month() + ' / ' + String.valueOf(m.year()));



      This will output a date with the first day and the given month.



      enter image description here






      Other solution (@Sebastian Kessel) - just use a Date instead of DateTime:



      Date startDate = Date.parse('5/30/2019');
      Date endDate = startDate.addYears(1);

      Integer durationInMonths = StartDate.MonthsBetween(EndDate);

      for (Integer i = 0; i <= durationInMonths; i++)
      Date m = Date.newInstance( StartDate.Year(), StartDate.Month(), 1);

      m = m.addMonths(i);






      share|improve this answer



























        2












        2








        2







        Easy solution that I want to post here - don't use addMonths. It looks like it just adds 30 days to the date, which isn't a valid solution in the slightest and produces problems like above. My mistake was assuming that addMonths wouldn't modify the day part of the date, which it totally will!



        Instead, just modify the month given to the constructor.



        Date startDate = Date.parse('5/30/2019');
        Date endDate = startDate.addYears(1);

        Integer durationInMonths = StartDate.MonthsBetween(EndDate);

        for (Integer i = 0; i <= durationInMonths; i++)
        DateTime m = DateTime.newInstance( StartDate.Year(), StartDate.Month() + i, 1);

        System.debug(i);
        System.debug(m.Day() + ' / ' + m.Month() + ' / ' + String.valueOf(m.year()));



        This will output a date with the first day and the given month.



        enter image description here






        Other solution (@Sebastian Kessel) - just use a Date instead of DateTime:



        Date startDate = Date.parse('5/30/2019');
        Date endDate = startDate.addYears(1);

        Integer durationInMonths = StartDate.MonthsBetween(EndDate);

        for (Integer i = 0; i <= durationInMonths; i++)
        Date m = Date.newInstance( StartDate.Year(), StartDate.Month(), 1);

        m = m.addMonths(i);






        share|improve this answer















        Easy solution that I want to post here - don't use addMonths. It looks like it just adds 30 days to the date, which isn't a valid solution in the slightest and produces problems like above. My mistake was assuming that addMonths wouldn't modify the day part of the date, which it totally will!



        Instead, just modify the month given to the constructor.



        Date startDate = Date.parse('5/30/2019');
        Date endDate = startDate.addYears(1);

        Integer durationInMonths = StartDate.MonthsBetween(EndDate);

        for (Integer i = 0; i <= durationInMonths; i++)
        DateTime m = DateTime.newInstance( StartDate.Year(), StartDate.Month() + i, 1);

        System.debug(i);
        System.debug(m.Day() + ' / ' + m.Month() + ' / ' + String.valueOf(m.year()));



        This will output a date with the first day and the given month.



        enter image description here






        Other solution (@Sebastian Kessel) - just use a Date instead of DateTime:



        Date startDate = Date.parse('5/30/2019');
        Date endDate = startDate.addYears(1);

        Integer durationInMonths = StartDate.MonthsBetween(EndDate);

        for (Integer i = 0; i <= durationInMonths; i++)
        Date m = Date.newInstance( StartDate.Year(), StartDate.Month(), 1);

        m = m.addMonths(i);







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 7 hours ago

























        answered 8 hours ago









        battery.cordbattery.cord

        7,29551848




        7,29551848



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Salesforce Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f265350%2fdatetime-addmonths-skips-a-month-from-feb-to-mar%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Invision Community Contents History See also References External links Navigation menuProprietaryinvisioncommunity.comIPS Community ForumsIPS Community Forumsthis blog entry"License Changes, IP.Board 3.4, and the Future""Interview -- Matt Mecham of Ibforums""CEO Invision Power Board, Matt Mecham Is a Liar, Thief!"IPB License Explanation 1.3, 1.3.1, 2.0, and 2.1ArchivedSecurity Fixes, Updates And Enhancements For IPB 1.3.1Archived"New Demo Accounts - Invision Power Services"the original"New Default Skin"the original"Invision Power Board 3.0.0 and Applications Released"the original"Archived copy"the original"Perpetual licenses being done away with""Release Notes - Invision Power Services""Introducing: IPS Community Suite 4!"Invision Community Release Notes

            Canceling a color specificationRandomly assigning color to Graphics3D objects?Default color for Filling in Mathematica 9Coloring specific elements of sets with a prime modified order in an array plotHow to pick a color differing significantly from the colors already in a given color list?Detection of the text colorColor numbers based on their valueCan color schemes for use with ColorData include opacity specification?My dynamic color schemes

            Tom Holland Mục lục Đầu đời và giáo dục | Sự nghiệp | Cuộc sống cá nhân | Phim tham gia | Giải thưởng và đề cử | Chú thích | Liên kết ngoài | Trình đơn chuyển hướngProfile“Person Details for Thomas Stanley Holland, "England and Wales Birth Registration Index, 1837-2008" — FamilySearch.org”"Meet Tom Holland... the 16-year-old star of The Impossible""Schoolboy actor Tom Holland finds himself in Oscar contention for role in tsunami drama"“Naomi Watts on the Prince William and Harry's reaction to her film about the late Princess Diana”lưu trữ"Holland and Pflueger Are West End's Two New 'Billy Elliots'""I'm so envious of my son, the movie star! British writer Dominic Holland's spent 20 years trying to crack Hollywood - but he's been beaten to it by a very unlikely rival"“Richard and Margaret Povey of Jersey, Channel Islands, UK: Information about Thomas Stanley Holland”"Tom Holland to play Billy Elliot""New Billy Elliot leaving the garage"Billy Elliot the Musical - Tom Holland - Billy"A Tale of four Billys: Tom Holland""The Feel Good Factor""Thames Christian College schoolboys join Myleene Klass for The Feelgood Factor""Government launches £600,000 arts bursaries pilot""BILLY's Chapman, Holland, Gardner & Jackson-Keen Visit Prime Minister""Elton John 'blown away' by Billy Elliot fifth birthday" (video with John's interview and fragments of Holland's performance)"First News interviews Arrietty's Tom Holland"“33rd Critics' Circle Film Awards winners”“National Board of Review Current Awards”Bản gốc"Ron Howard Whaling Tale 'In The Heart Of The Sea' Casts Tom Holland"“'Spider-Man' Finds Tom Holland to Star as New Web-Slinger”lưu trữ“Captain America: Civil War (2016)”“Film Review: ‘Captain America: Civil War’”lưu trữ“‘Captain America: Civil War’ review: Choose your own avenger”lưu trữ“The Lost City of Z reviews”“Sony Pictures and Marvel Studios Find Their 'Spider-Man' Star and Director”“‘Mary Magdalene’, ‘Current War’ & ‘Wind River’ Get 2017 Release Dates From Weinstein”“Lionsgate Unleashing Daisy Ridley & Tom Holland Starrer ‘Chaos Walking’ In Cannes”“PTA's 'Master' Leads Chicago Film Critics Nominations, UPDATED: Houston and Indiana Critics Nominations”“Nominaciones Goya 2013 Telecinco Cinema – ENG”“Jameson Empire Film Awards: Martin Freeman wins best actor for performance in The Hobbit”“34th Annual Young Artist Awards”Bản gốc“Teen Choice Awards 2016—Captain America: Civil War Leads Second Wave of Nominations”“BAFTA Film Award Nominations: ‘La La Land’ Leads Race”“Saturn Awards Nominations 2017: 'Rogue One,' 'Walking Dead' Lead”Tom HollandTom HollandTom HollandTom Hollandmedia.gettyimages.comWorldCat Identities300279794no20130442900000 0004 0355 42791085670554170004732cb16706349t(data)XX5557367