/  Technology   /  How to work with Google Spanner Date type in Javascript?
How to work with Google Spanner Date type in Javascript?

How to work with Google Spanner Date type in Javascript?

 

We can use the FORMAT_DATE function to format the string containing a date expression. It is one of the highly used javascript functions to format date. This function is equivalent to the to_char and to_date function present in other relational databases. In this article, we will check the cloud Spanner FORMAT_DATE function,

The format_date function is used to date the expression to the required format.

Cloud Spanner FORMAT_DATE function formats the date expression containing the date according to the specified date format string.

Syntax:

FORMAT_DATE(format_string, date_expr)

JAVA:

static void queryWithDate(DatabaseClient dbClient) {
  String exampleDate = "2019-01-01";
  Statement statement =
     Statement.newBuilder(
          "SELECT VenueId, VenueName, LastContactDate FROM Venues "
                  + "WHERE LastContactDate < @lastContactDate")
          .bind("lastContactDate")
          .to(exampleDate)
          .build();
  try (ResultSet resultSet = dbClient.singleUse().executeQuery(statement)) {
    while (resultSet.next()) {
      System.out.printf(
          "%d %s %s\n",
          resultSet.getLong("VenueId"),
          resultSet.getString("VenueName"),
          resultSet.getDate("LastContactDate"));
    }
  }
}

Node JS :

// Imports the Google Cloud client library.
const {Spanner} = require('@google-cloud/spanner');
/**
 * TODO(developer): Uncomment the following lines before running the sample.
 */
// const projectId = 'my-project-id';
// const instanceId = 'my-instance';
// const databaseId = 'my-database';
// Creates a client.
const spanner = new Spanner({
  projectId: projectId,
});
// Gets a reference to a Cloud Spanner instance and database.
const instance = spanner.instance(instanceId);
const database = instance.database(databaseId);
const fieldType = {
  type: 'date',
};
const exampleDate = '2019-01-01';
const query = {
  sql: `SELECT VenueId, VenueName, LastContactDate FROM Venues
          WHERE LastContactDate < @lastContactDate`,
  params: {
    lastContactDate: exampleDate,
  },
  types: {
    lastContactDate: fieldType,
  },
};
// Queries rows from the Venues table.
try {
  const [rows] = await database.run(query);
  rows.forEach(row => {
    const date = row[2]['value'];
    const json = row.toJSON();
    console.log(
      `VenueId: ${json.VenueId}, VenueName: ${json.VenueName},` +
        ` LastContactDate: ${JSON.stringify(date).substring(1, 11)}`
    );
  });
} catch (err) {
  console.error('ERROR:', err);
} finally {
  // Close the database when finished.
  database.close();
}

JAVASCRIPT:

rows.forEach(row => {
const date = row[2]['value'];
const json = row.toJSON();
console.log(
`VenueId: ${json.VenueId}, VenueName: ${json.VenueName},` +
` LastContactDate: ${JSON.stringify(date).substring(1, 11)}`
);});

 

Leave a comment