0

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

Is there any built in function available?

2 Answers2

3
// 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.

pushkin
  • 9,575
  • 15
  • 51
  • 95
Paul O'Mahony
  • 6,740
  • 1
  • 10
  • 15
  • Hi Paul. I am asking from Query language perspective. Is there any way to do it by Hyperledger Composer Query language ? – Parth Raval Aug 28 '17 at 04:55
  • No it is not possible from the [query language](https://hyperledger.github.io/composer/latest/reference/query-language.html). Also this would not be deterministic in terms of code execution. You'd need to pass a date to the transaction in order to be sure that it is executed on every peer the same way. – edi Oct 15 '18 at 21:27
1

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

Paul O'Mahony
  • 6,740
  • 1
  • 10
  • 15