Help calculating date difference using datetimeparse

Hi. I'm hoping someone can help me with this snippet cos I’ve searched online for hours and can’t find any info on how to actually use datetimeparse() to fix it.

I understand that the dates all need to be in this format, YYYY-MM-DD, for date difference to work and it does work when I have all the dates set this way (as per first screen shot). However, I'd like the baby’s date of birth formatted differently. I’ve read that this can be done using datetimeparse (and I can see that in the error message) but I just don’t how, or where, to add it.

Ideally I’d like it to look like this:

Baby’s name: Brad

Baby’s DOB: 04/02/23

Partner’s name: Paul

Other Child/ren: Copied from clipboard

Baby’s current age: 5 weeks 2 days - is there a way to calculate the weeks and days rather than just the days?

The dates used to calculate baby’s current age are the DOB (first date) and the date of the appointment (second date). If there is a way for this formula not to be shown when the snippet is used that would be amazing, if not that’s ok cos I can easily delete it.



Thank you in anticipation.

~ Bec

Hi @Bec_Dawson welcome back and interesting question(s)! I have answered all your questions in this snippet:

{note: trim=yes}
Baby's DOB: {formdate: MM/DD/YYYY; name=DOB; default=02/05/2023}
Second date: {formdate: YYYY-MM-DD; name=date2}
{dob_formatted=datetimeparse(dob, "MM/DD/YYYY")}
{difference=datetimediff(dob_formatted, date2, "D")}
{endnote: trim=yes}
The baby's age is {=difference} days. This is {=weeks} weeks and {=days} days.{weeks=floor(difference / 7)}{days=remainder(difference, 7)}

You can use the {note} command as I did above.

Yes! You can lookup the functions I have used on our math function reference page.

You can check the snippet above for reference on that as well.

Hi @Gaurang_Tandon, that is so helpful, it was easy for me to alter slightly to display exactly how I was hoping for it to... Thank you very much!!

I understand what you did with the note command but the rest is way over my head I'm afraid :laughing:

I'm sorry but I have one further question - I'd like to only have to enter the baby's DOB once, rather than twice (one DOB is for my notes and the other is for the formula as per attached screenshot). I worked out how to link the two DOB's but I can't make the one that appears on my notes display as DD/MM/YY

Thanks again!

~ Bec

Sure. You can use datetimeformat to do that. Here's an example (I just included the relevant parts from my previous snippet):

Baby's DOB: {=datetimeformat(dob_formatted, "DD/MM/YYYY")}
{note}
Baby's DOB: {formdate: MM/DD/YYYY; name=DOB; default=02/05/2023}
{dob_formatted=datetimeparse(dob, "MM/DD/YYYY")}
{endnote: trim=yes}

Perfect! Thank you :blush: