Parth Raval
Parth Raval

Reputation: 15

How to get Current Date in Hyperledger Composer Query language

How to Compare DateTime Data type with current date in hyperledger composer?

Is there any built in function available?

Upvotes: 0

Views: 2348

Answers (2)

Paul O'Mahony
Paul O'Mahony

Reputation: 6740

// get a date object
var now = new Date();

DateTime is normally defined like this.

YYYY-MM-DDThh:mm:ss

The easiest way to compare dates in Javascript is to first convert the DateTime value to a Date object and then compare these date-objects.

you can use

dates.compare(a,b) // where a and b are date objects 

or

var dateString = "2050-01-02T11:42Z"; //DateTime
var myDate = new Date(dateString);

var now = new Date();
if (now < myDate) {
     document.write('myDate is in the future');
}
else
{
    document.write('myDate is NOT in the future');
}

or see here

Compare two dates with JavaScript

Obviously doing a new Date() won't be really appropriate in a Transaction Processor as its being deployed to a runtime blockchain and knowing when its evaluated after being endorsed/executed is anyone's guess.

Upvotes: 3

Paul O&#39;Mahony
Paul O&#39;Mahony

Reputation: 6740

Yes. Given principles of my original answer still remain - this is an example of how to pass it in as a parameter.

var now = new Date();

now.setMinutes(10); // set the date to be time you want to query from (example)

var q1 = businessNetworkConnection.buildQuery('SELECT org.hyperledger.composer.system.HistorianRecord ' + 'WHERE (transactionTimestamp > _$justnow)');

return businessNetworkConnection.query(q1,{justnow:now});

The date time string may be in a simplified ISO 8601 format. For example, "2016-10-10" (just date) or "2017-10-10T14:48:00" (date and time) can be passed and compared against a Composer DateTime field.

also see here - https://hyperledger.github.io/composer/business-network/query.html

Upvotes: 1

Related Questions