NVelocity-Work with dates within templates??

For general questions and discussions specific to the AbleCommerce GOLD ASP.Net shopping cart software.
Post Reply
User avatar
AnySupport
Lieutenant (LT)
Lieutenant (LT)
Posts: 73
Joined: Fri Feb 17, 2012 8:58 am

NVelocity-Work with dates within templates??

Post by AnySupport » Wed Jul 08, 2020 2:28 pm

I figure this is a long shot, but the lack of documentation on NVelocity itself is quite amazing... I've been grasping at straws, but not much works. At this point I'm using the Wayback Machine to try to find more references, which has helped some, but it's still rather hit & miss.
------
I am only working within an email template, and not the code that would build & define any variables behind the scenes.
I have two custom variables that are both DateTime type.
And.... well, when I started writing this, I had thought that I was successfully able to compare them to each other broadly to see which one was greater, and that I was only missing some way to get more specific differences from them.
(e.g: to find the timelength between (in whatever value it would give me to work with) )
But then I re-checked my if/then/else statements and now I'm doubting that even my broad comparison was actually working. Argh.

So, is there any way to work with dates from the template itself?
Maybe some syntax I'm just missing or some set of methods that are usable just not spelled out?

Or does any work involving dates to be used in an email template have to occur entirely within the code, leaving the template only the final result?

Any guidance (or even direction to a resource to read through) would be just massively appreciated!
Thank you!

User avatar
AnySupport
Lieutenant (LT)
Lieutenant (LT)
Posts: 73
Joined: Fri Feb 17, 2012 8:58 am

Re: NVelocity-Work with dates within templates??

Post by AnySupport » Wed Jul 08, 2020 4:27 pm

Aaand of course, within an hour or two of finally posting a request for help, I found what I needed and it seems to work with NVelocity!
So, I'll leave this here for anyone else who might be caught in the gray area of working with NVelocity but not necessarily a C# programmer... :)
---------------
TimeSpan object :Microsoft Docs (for Core 3.1?) I admit I don't really know if it's the completely correct version to reference, but it seems to be close enough for what I need. The quote is taken from the linked Microsoft Docs page.
Remarks
A TimeSpan object represents a time interval (duration of time or elapsed time) that is measured as a positive or negative number of days, hours, minutes, seconds, and fractions of a second. ... The largest unit of time that the TimeSpan structure uses to measure duration is a day. Time intervals are measured in days for consistency, because the number of days in larger units of time, such as months and years, varies.
Context Notes of my Usage (vs whatever might be yours)
  • This was in a loop where Date1 might or might not exist.
  • Date2 stayed the same in any loop iteration.
  • I knew that with my current test example that when Date1 did exist, Date2 was newer than Date1.
    (So, more work will be needed to make the template cleanly handle when Date2 could be older than Date1.)
  • If Date1 doesn't exist, it just used 01/01/0001.
    (So you don't have to cover for math with a NULL, but you will have to check for really really large differences!)
This was all I needed. (generalized,just in case someone else is looking for similar answers and needs the visual. Hope this will help!)

Code: Select all

#set ($timediff = $Date2.Subtract($Date1) )

Difference: $timediff.Days()
So, with my Date2 being steady and Date1 either being older or non-existent, this would give me 737590 (days) or 1158 (days).
That's certainly a large enough difference that will make it easy enough to determine out-of-range timespans for when Date1 doesn't exist.

---------------

Enjoy!(?) :lol:

P.S. Another "just in case": this was the article that I found that suddenly clicked. It has a very very basic demonstration of Days() (which returns integers) and TotalDays() (which returns fractions).

User avatar
AnySupport
Lieutenant (LT)
Lieutenant (LT)
Posts: 73
Joined: Fri Feb 17, 2012 8:58 am

Re: NVelocity-Work with dates within templates??

Post by AnySupport » Fri Aug 14, 2020 10:37 am

Hello all again!

I had been able to fix one part of my issue with that reference above, because I had two custom field dates to compare by using subtraction tools.
However, I'm now stumped in a different template because I only have one of those dates available in this one.

but I have to check if that date is before today (the day of the email being sent) and for some reason, I just cannot figure out how to get the current date! This seems like it would be something beneficial in many scenarios of emails and conditionally coding, so I have to believe it exists in some way. But I'm stumped and have to ask again for help!

I've tried printing with all of these and just get them all printed as text.

Code: Select all

<b>System Time:</b>  $System.DateTime.Now.ToShortDateString() || $DateTime.Now()  || $Now
<b>dateTool :</b> $dateTool 
$DateTime.Today  ||  $DateTime.Today.ToString("d") <br/>
#set ($datevar = $DateTime.Today )
$datevar ||  $datevar.ToString("d")  || $datevar.GetType()
I've been hunting as many places as I can for NVelocity or even just C# shortcuts that might still work, and am coming up with nothing.

Is there any known way to get the current date from within template coding?
(So, not requiring programming yet another custom field behind the scenes just to pull it in advance? )

Please help! I feel like there has to be some call that I'm just completely missing. :?
Thank you!

User avatar
Naveed
Rear Admiral (RADM)
Rear Admiral (RADM)
Posts: 611
Joined: Thu Apr 03, 2008 4:48 am

Re: NVelocity-Work with dates within templates??

Post by Naveed » Tue Aug 18, 2020 5:45 am

Hi,

I think you need to define a utility function on one of the C# classes already in the context. That utility function can be called to return you the current date.



Here is some nvelocity references for help, unfortunately they do not discuss the DateTime issues you want to resolve.

http://help.ablecommerce.com/mergedProj ... erence.htm

http://wiki.ablecommerce.com/index.php/NVelocity

Thanks,

User avatar
AnySupport
Lieutenant (LT)
Lieutenant (LT)
Posts: 73
Joined: Fri Feb 17, 2012 8:58 am

Re: NVelocity-Work with dates within templates??

Post by AnySupport » Tue Aug 18, 2020 1:41 pm

I did have those and had read through them, but thank you, Naveen.

I had been trying to get this to work with whatever already existed in NVelocity or AbleC's adaption of it, etc. Maybe some hidden term, but no luck so I've had to ask for a custom field to get added in the end anyway. (Figured I had to try though)

Thanks!

Post Reply