LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How do I add 1 month to a timestamp date?

So I need to create a series of date based on a start date, in 1 month timesteps.

 

I thought this would be easy, but right now I can't find any method to simply increment the month on a timestamp.  The only options I can find are to add either seconds, minutes, hours, or years to a timestamp (see http://thinkinging.com/2007/06/08/labview-tip-easy-relative-timestamp-calculations/) or to convert to a date/time record, increment the month field, and then convert back (as shown here http://digital.ni.com/public.nsf/allkb/F611BA836BF738EE86256EE6006DF459).  The problem with the first method is that there is no month unit (understandably so - a month is not a constant duration, but then, neither is a year....) and the problem with the second is that it doesn't handle date boundaries correclty (i.e. adding 1 month to december results in a date/time record with month = 13, so converting to a timestamp does not give a valid date).

 

Is the only solution to write a calendar object which handles all of the date-math complexity internally?  Does someone have such an object they can share?  Or am I stuck writing my own?

 

Thanks!

0 Kudos
Message 1 of 27
(12,064 Views)

Looks like you're stuck writing your own (unless you want to use .NET or something like that) - I would calculate the number of seconds in the month (by creating and subtracting two timestamps - which should account for things like leap years / different numbers of days in the month etc.) and then add that to your current timestamp.

 

Edit: Actually, now that I look at it, I think it's a bug/unexpected behaviour in the Date/Time Rec to Seconds function - if you add 30 days to 'day of month' it automatically rolls over the month and if you add 300 days, it rolls over the month & year. I would have expected the same behaviour for adding to the month.

 

It is documented though: "If year and month are out of range, the results are unpredictable" - Well jeez...that's helpful!


LabVIEW Champion, CLA, CLED, CTD
(blog)
Message 2 of 27
(12,055 Views)

I'd like a few more details on what you want to happen.  So if I have December 20th 2015 and I add a month do you want Januaray 20th 2016?  What do you want if I have January 30th 2016 and add a month?  February 30th?

 

If you just want to add a set number of days that is quite easy and is attached, all it takes is an Add and a double constant formatted to represent the display days.

 

Oh so I guess an the easiest method of doing this would be to add the number of days of that month, so if you are in December add 31, if you are in February add 28 (or 29) days.  It still might not be right but I think this would be close to what you want.

0 Kudos
Message 3 of 27
(12,035 Views)

Hoovah-

I would like to increment the month, keeping all other values the same, but while also handling all the truncating/rollover problems.

 

So 12/29/2015 + 1month = 01/29/2016

01/29/2015 + 1 month = 02/28/2015

01/15/2015+1 month = 02/15/2015.

 

Although my immediate need is to increment the month, it is strange to me that such date manipulation is so difficult in labview compared to other languages.  Java has a nice calendar utility object which handles all of this for you, including leap year calcs etc.  If a developer needs that functionality, they just import the library.  I guess I wish labview publised such librairies, or a user group provided user made libraries of the sort.

0 Kudos
Message 4 of 27
(12,002 Views)

@rothloup wrote:

I guess I wish labview publised such librairies, or a user group provided user made libraries of the sort.


Well, when you've written one, share it so that others can benefit Smiley Very Happy


LabVIEW Champion, CLA, CLED, CTD
(blog)
0 Kudos
Message 5 of 27
(11,993 Views)

What a sharp response, Sam!    Smiley Tongue

0 Kudos
Message 6 of 27
(11,974 Views)

Hi rothloup,

 

I would like to increment the month, keeping all other values the same, but while also handling all the truncating/rollover problems.

Your examples are flawed!

According to your text description you want to convert 01/29/2016 to 02/29/2016, but in your example data you convert to 02/28/2016.

 

That's why there is no "easy-to-use" library: everyone uses different conventions!

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 7 of 27
(11,966 Views)

@rothloup wrote:

What a sharp response, Sam!    Smiley Tongue


Well it is honest and true. Many people come here and complain about this and that not working the way they like. If they get a good answer they walk often away without even saying thanks. If they develop something themselves after having complained that this is the super duper sliced bread standard function every programmer would need, they don't come back to post their solution either to help others who might feel the same. So not so surprising that you may get such answers after such a post. Smiley LOL

 

And I would have to agree with Gerd, your own requirements description is contradicting itself. So there is not only no "standard" function which could implement that but none at all. It would also depend a lot what you intend to do with this date difference. For generating log files for instance it would seem more logical to simply switch to a different filename/directory whenever the month changes rather than after a month from an arbitrary start time.

Rolf Kalbermatter
My Blog
0 Kudos
Message 8 of 27
(11,956 Views)

Upon reading your edit, I'm inclined to agree with you - I've used the end-of-the-month rollover before and I guess I expected that the end of year rollover would operate the same way.

0 Kudos
Message 9 of 27
(11,947 Views)

I don't see the contradiction in my examples vs. my text.

 

I said that I want the month to increment by one (all of my examples do that).

And that it should handle truncating and rollover.  Perhaps I should have been more specific that day of the month should be truncated while end of the year should rollover.

 

In any case, I have my answer, thank you all.

0 Kudos
Message 10 of 27
(11,943 Views)