How to get the current date and time?

Good day to all of you!


An issue that we run into again and again in our scripting is the need to use today's date and time for part of scripting (usually to determine how many days/months ago something happened). We have the code from the BI Cheats thread on how to determine the number of days / months between two dates but we cannot figure out how to get today's date to use in those equations. If anyone can let me know how to get the current date and time (together or separate) it would be greatly appreciated!


Have a great day everyone!


Tom

There are various ways you could go about this. You can see one example in the Dundas BI Script Library using Date():

Using a Timer Control


Another option is to use the Date.now JavaScript function. This returns a number in milliseconds, which you will have to manipulate a bit. You can read about it and see some examples from the MSDN docs.


You also have the option to use the pre-defined Dundas BI tokens, which you can find in the API documentation. For example:

dundas.data.PredefinedTokens.CURRENT_DAY


Hope this helps.

Thanks for the suggestions. Do you know how to get any of those to work in a Calculated Element? When I try this (from the TimerControl example):

var dt = new Date();

it tells me there is an error in column 18 (ie. between the e in date and the open parenthesis).


When I try this (from MSDN docs):

var start = Date.now();

it tells me there is an error in column 11 (just before the = sign);


When I try the this code you provided (from API documentation):

var start = dundas.data.PredefinedTokens.CURRENT_DAY;

it tells me there is an error in column 11 (just before the = sign).


Any additional assistance would be greatly appreciated.


Tom

Calculated elements are a bit different. While the dashboard scripting is done using JavaScript, calculated elements take DundasScript so the JavaScript code won't work.


DundasScript is a subset of C#, so you have to use fitting code. Depending on what exactly you need, try DateTime.Now, DateTime.Today, or DateTime.UtcNow.

Thank you very much Ella!


Using the DateTime.Now function has given us the current date which has allowed us to determine the differece between two dates in either days or months! We are very excited to do this in a bunch of our cubes now.


Difference in Days is being done as follows:

var currentDate_asDate = DateTime.Now;

int currentDate_asDays = currentDate_asDate.Year * 365 + currentDate_asDate.DayOfYear;

int dateOfInterest_asDays = $DateOfInterest$.Year * 365 + $DateOfInterest$.DayOfYear;

return currentDate_asDays - dateOfInterest_asDays;


Similarly, to get the difference in Months:

var currentDate_asDate = DateTime.Now;

int currentDate_asMonths = currentDate_asDate.Year * 12 + currentDate_asDate.Month;

int dateOfInterest_asMonths = $DateOfInterest$.Year * 12 + $DateOfInterest$.Month;

return currentDate_asMonths - dateOfInterest_asMonths;


Once again, thank you very much for your assistance.


Have a great day everyone!


Tom

1 Like