Home
About
Search
Welcome to M-Files Empower – our new support experience. We'd love to hear what you think!Give feedback
Home

Add Months, Years, or Days to a Date Value (VBScript)

Last updated on 17 September 2021

Metadata Card Configuration

Overview

If a specified number of months, years, or days is needed to be added to a date value, this can be done by using DateAdd() in Calculated Values (VBScript).

Solution

A calculated value for a property definition can be set up, using a VB script with the DateAdd() function, to adds a number of months, years, or days to the initial value. For an example of this, this sample script adds 2 months to an original date:

DIM dateValue
dateValue = PropertyValues.SearchForProperty( 1255).TypedValue.DisplayValue 'ID of this property
 
if (dateValue ="" ) then 'Check if the value already exist or is it empty
    DIM origDate
    origDate = PropertyValues.SearchForProperty( 1256).TypedValue.DisplayValue 'property which has original date
    Output = DateAdd("m",2,origDate)
end if



To ensure that this VB script is able to add 2 months to the date, please also follow these instructions:


- You must place the code in the settings for the property definition's Calculated Value (VBScript), as explained in our guide linked here:
https://www.m-files.com/user-guide/latest/eng/Automatic_values.html 


- You must change value 1255 (above) to the ID value of your property (where the new value will be shown).

- You must change value 1256 (above) the to ID value or your property which has the original date.

- The script updates the value only once. If you want that property value to continuously be updated when the original date changes, then simply remove "if" and "end if" lines from the script.

- Both properties must exist in the same metadata card.

Lastly, the DateAdd() function can be modified to add a number years or days. See below for examples.

To add 2 years, the line above can be changed to this format:
Output = DateAdd("yyyy",2,origDate)

To add 2 days, the line above can be changed to this format:
Output = DateAdd("d",2,origDate)

 

Still need help?

On this page