Return todays date, and subtract days

I need to return todays date, as a DateTime object, then I want to subtract days from it to return the date from “in_days” Ago. So if someone wants a credit for one week, I want to credit them from 7 days ago up to todays date. I haven’t messed around with DateTime objects too much so any help is appreciated.

Also what are the differences/Best uses for System.DateTime and Microsoft.VisualBasics.DateAndTime?

Assuming you don’t care about the time portion (this will return the datetime specified at midnight):

Assign YourDate = today.AddDays(-in_days)

Change today → now if you want the current hour/min/second included.

As for your second question, you should use System.DateTime. I’m not sure what the DateAndTime object is.

3 Likes

So this is what I’m trying to do.

today.AddDays(Double.Parse(-in_CreditDays))

This is the error I’m getting now…

Option Strict On disallows implicit conversions from String to double.

My in_CreditDays is a string coming in from the queue. I’m trying to convert it to a double.
I guess It doesn’t like how I’m doing it.

UPDATE:
So I assigned the in_CreditDays to a double, and used the exact same Double.parse Method and it worked.
I’m guessing the compiler was confused with the arguments I put in all at once.

1 Like

I think it’s because you put a negative sign next to a String. You would want to place -1* next to the Double. Parse with no negative inside. So, it converts it to a double first, then multiplies it by -1

Note: if in_CreditDays is already a double type, then you don’t need to use Double.Parse()
today.AddDays(-1*in_CreditDays) would be enough if it’s already a Double prior to that code.

1 Like

Awesome. I’ll switch it now and see if that works.

Oh also, you should use an Integer for subtracting days. A Double type contains decimals.
Like for example,
today.AddDays(-1.1) is the same as today.AddDays(-2), but different than today.AddDays(-1)

1 Like

So how do get rid of the time after the date. I just want the date…

today.AddDays(-1).ToShortDateString
or if you want your own format use this:
today.AddDays(-1).ToString("MM/dd/yyyy")

Sorry if I copy Dave’s wouldbe answer, lol.

Regards.

5 Likes

good catch! forgot about that.

@HsDev - a datetime will always include both date and time. If you want to display as a string, you can change the format of it so it only shows the date by doing DateTime.Tostring(“MM/dd/yyyy”)

You can change the format however you’d like, see the formatting options available here: Custom date and time format strings | Microsoft Learn

1 Like

Okay, so I’m using set Text to enter the date ranges From and To date for the time OOS. the automation works… but it takes an abnormal amount of time… Like 20 seconds to enter the date into the field. Type into doesn’t work. It’s a field next to a little pop up calendar button that also services as a calculator. So when you enter the dates, it should update the ending credit total to match the days OOS. It just takes FooorEEEver

It could be that IE doesn’t work well with the application. In those cases, you can try setting the WaitForReady property to “None”.

Hopefully, that solves the slowness.

Regards.

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.