LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Adding and subtracting date strings

Is there a simple way to add or subtract from a date and or time???


I current have a date time string I wish to subtract 2 days from the date and some time I need to add 2 months to the date.

is there a simple way to accomplish this??
0 Kudos
Message 1 of 12
(7,087 Views)
You can convert the strings to LV time format, perform your operations using numeric operators, and then convert it back to string. To do this, from the Time & Dialog pallette, take the Seconds to date\time VI and place it on the diagram. Place a "Bundle by name" function near it and wire the output of the VI into "input cluster". Wire your data in, convert the result back using Date\time to seconds and do your calculations. Then, take the reverse path. You may want to write a subVI to do this.

___________________
Try to take over the world!
Message 2 of 12
(7,082 Views)
I forgot to say, you can find VIs for converting the strings to numbers in the string palette.
By the way, you can do the calculations on the numbers directly, but it's really not worth it, due the fact that you have to sync months\days\years and so on.

___________________
Try to take over the world!
Message 3 of 12
(7,078 Views)
I have same kind of problem... How should I convert that time string to LV time or numeric.. I have program where I have to type two clocktimes in format HH:MM:SS,ss and substract those. I can´t use decimal string to numeric becouse then only the first numbers get through?
0 Kudos
Message 4 of 12
(7,047 Views)
I must be missing something I have not had any luck so far could you provide an example possibly??
0 Kudos
Message 5 of 12
(7,013 Views)
Tst gave most of the solution, but forgot to explain how to convert string to date...

Adding or subtracting days is rather easy. Dealing with months is another story : 28, 29, 30, 31 days ?
Seems that in this case it should be better to work directly on the month index value, and to manage the year changes. Still, there is an issue : how to add a month to March the 31th ? There is no 31th April !

The attached vi does most of the job, but the question about the above issue is not answered...

Hope this helps
Chilly Charly    (aka CC)

         E-List Master - Kudos glutton - Press the yellow button on the left...
        
0 Kudos
Message 6 of 12
(6,985 Views)
Here's a VI I started putting together to convert time strings to LV format. It's not finished, but the general idea is there and can be modified.
To add months, I would probably use a case structure for all the months with 30 days (4,6,9,11) and for February based on leap years (mod 0 from dividing by 4) and would use In range to keep them to 30.
The attached VI can also be used to subtract months directly in LV format and LV will handle the conversion on its own (BTW, since it appears to work on # of seconds, adding a month to 31/3 will get you 1/5).

___________________
Try to take over the world!
0 Kudos
Message 7 of 12
(6,971 Views)
Hi CC,

As your demo shows, April 31 = May 1.

If this is not acceptable and the "month delta" is a fixed number of days, we run into another issue that is present in you example but not very obvious.

The issue is day light savings time. Do y'all do that in Europe?

It turns out that there are two days in the year that do not have 24 hours. One has 25 and the other has 23.

I got bit by this last fall when I was trying to shcedule an event to occur at the next occuring midnight. I though I could get away with adding 24 hours worth of seconds to the current time, then "round down to midnight" by using the format time, set hour and second to "0" and then convert back.

Well when the calendar clicked past midnight of the day the clocks were getting set back (the 25 hour day) I called the sub-VI at just after midnight. Well, adding 24 hours was not enough to get me to the next day. The result was that I got the midnight of "today" instead of tomorow.

Yes the application hung!

The customer I was working with went off and developed a VI that did this correctly by using an iterative approach that looked for the time day to change.

So....

1) The example you posted is prefered method of doing this.

2) There are not 24 hours in every day.

Now for a mind numbing ancillary observation:
File written at midnight durring daylight savings time are time stamped as midnight only as long as DST is active. If you check the creating time when DST is not active, there modify time shifts by an hour.

This is similar to the "April 31 = May 1" relation your example demonstrates.

Funny thing time...

Ben
Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
Message 8 of 12
(6,971 Views)
Hi tst,

We had a post collision!

I did not mean to step on your post.

I will see if I can get my customer to post his solution (He is another contributor to this Exchange).

no guarentees.

Ben
Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
Message 9 of 12
(6,961 Views)
Hi,

Handling 2 days is fine. Months as previously pointed out is tricky.

In the attached image we set the time back by 30 days (2592000 secs). Very simple and very effective.

Hope this helps.

- Gurdas
Gurdas Sandhu, Ph.D.
ORISE Research Fellow at US EPA
0 Kudos
Message 10 of 12
(6,748 Views)