LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

DateAdd(interval,number,date) Visual Basic to LabVIEW Time Stamp add 18 months

Hi all,
 
I need to match Visual Basic Cmd  =  DateAdd("m", 18, Date()) which adds 18 months to today's date.  When I use LabVIEW Time Stamp VI's at first I tried adding 1.5 years in seconds. but this did not match the VB generated method?  What method do  I use in LV to obtain the VB DateAdd("m", 18, Date())

Example 'Add one month to January 31, 2000

document.write(DateAdd("m",1,"31-Jan-00"))

Output:

2/29/2000

0 Kudos
Message 1 of 16
(23,655 Views)
Do you have access to the VB code (I am not familiar enough with VB to know if that is a standard function)?  If so, it may be worth looking at that code to try to replicate.

In the example you posted, the function added 29 days to the date.  I bet if you put July 31 as the date, you would have gotten Aug 31 out, which would have added 31 days to the date.  So the function is more complicated that just adding a fixed number of secs to a value.  The problem probably lies in the fact the the VB function takes into account the varying days in each month.  The program probably assumed February of 2000 had 29 days (2000 was not actually a leap year - the function gave an invalid date).  It then adjusted the value of a month accordingly.

So your best bet is gets your hands on source if available to mimmick the behavior.  It may be as simple as adding a 31 day month, then back tracking to the last date of the month if you roll over.  What does an input date of Jan 27, 2000 give you?  If it gives you Feb 27, then this is exactly what they are doing, because you would have moved back 4 days on your input and only gotten a change of 2 days on your output.  In that case, Jan 28 - Jan 31 would have the same result on non-leap years.

You may need to use Seconds to Date/Time and Date/Time to Seconds on the Time Pallete to do your math, but the functions don't handle rollover (32 days, 13 months).
0 Kudos
Message 2 of 16
(23,645 Views)
Well this is how you do it. However, it doesn't seem to work with the month. There was a thread about this problem not too long ago. If you overflow the month in the date/time rec cluster (so it is greater than 12), date/time to seconds returns 0. Every other element works fine. You can add 1000 to the day and it will figure it out just fine. When this bug is fixed, this will work. Also, it might only be a bug in recent versions. I'm on 8.5 and it doesn't work right.

I realize this isn't a great answer to your question, but as far as I know, this is the easiest way to do what you want and have LabVIEW handle all the work.


Message Edited by Marc A on 10-03-2007 03:56 PM

0 Kudos
Message 3 of 16
(23,639 Views)
The problem with the above is the end of the months.  If you use Jan 31, 2000 as your input, you'll get 3/2/2000 as your output with a 1 month addition.  Because 31 days is now a rollover and it moves into March.

So, you have to be smater than the simple addition to mimmick what the VB code is doing.
0 Kudos
Message 4 of 16
(23,634 Views)
Yeah I realized that after I posted. The other thing I was going to try was to use convert unit to convert a number to months and add that to the timestamp. There is no months unit though.

If this is just going to add months, it shouldn't be too much effort to create a function that does it. Use what I posted as a starting point and add some logic for cases when the month overflows. All you would really need to do is check what the new month is and if the day is greater than the number in that month, reduce it to the last day before rebundling the cluster. That would give you 1/31/00 + 1 month = 2/29/00.
0 Kudos
Message 5 of 16
(23,630 Views)
Perhaps you could do this, at least for a relatively simple case of adding 18 months.  More complicated additions might require more complicated comparisons.
 
If the starting month is 6 or less, add 1 year and 6 months.
If the starting month is greater than 6, add 2 years and subtract 6 months.
 
 
At least this would be a workaround until the bug in Labview is fixed.  In earlier versions, the bug affected more of the cluster.

Message Edited by Ravens Fan on 10-03-2007 09:11 PM

0 Kudos
Message 6 of 16
(23,617 Views)
Here is a link to the earlier thread about the issue with the month.
 
 
In it, I posted code that would correct a month that was less than 1 or greater than 12 so that the cluster would not raise the bug when it converted back to a timestamp.  This seemed to work whether adding or subtracting any amount of months and would be more generalized solution then what I listed above.

Message Edited by Ravens Fan on 10-03-2007 09:23 PM

0 Kudos
Message 7 of 16
(23,616 Views)
This is a Standard VB function = DateAdd(interval,number,date).   Visual Basic to LabVIEW Time Stamp add 18 months.  I had simuliar solution as Raven's and handled the "BUG" in months only counting to 12 in a simuliar mehtod.
 
Day in Time Stamp, also has a simuliar "BUG" like months, in that after adding 18 months to AUG 31st, LabVIEW Time Stamp would report FEB 31st.
 
To solve this
 
 
 
This works until 2076.
0 Kudos
Message 8 of 16
(23,274 Views)
Your picture didn't show.  The path to your image file was J:/Testdept/add%2018%20months.bmp"></DIV> (with the goofy html codes in their).  You need to first attach the image to your message, then go back and edit the message adding the image so that it's path points to the NI forum servers.
0 Kudos
Message 9 of 16
(23,271 Views)
Sorry Ravens Fan,
 
Once I saw the picture in the Preview, I assumed it would post OK.
 
I will try attaching the code -- small file instead.
 
Thanks
0 Kudos
Message 10 of 16
(23,258 Views)