sbaden
sbaden

Reputation: 565

How do I pre-populate the createDateBox() or date picker field with the current date?

I have created a Ui on a Google Sites page with a date field (date picker). Is there a way to pre-populate it with the current date? Secondly, is there a way of isolating the year from the date picked? Here is the code I have so far:

    //Create elements for vPanel_01
    var labelDate = app.createLabel("Date of event:");
    var textBoxDate = app.createDateBox().setName("date").setStyleAttribute("color", "#a7a7a7");

I'd like to isolate the year from the input so that I can use for comparisons in the script.

Upvotes: 0

Views: 1649

Answers (2)

Serge insas
Serge insas

Reputation: 46802

Following your comment : if I keep megabytes1024's example you should add an handler to the textBox (or to a button hat validates the input) and a callBackElement to this handler and then, in the handler function you can use something like this :

var dateObject = new Date(e.parameter.date); // 'date' is the name of the datepicker

When you have this date object you can get whatever you want from it, for example :

var fullyear = dateObject.getFullYear(); // will return full year in 4 digits

see doc on date object

You could be interested in this example as well.

Upvotes: 0

megabyte1024
megabyte1024

Reputation: 8660

Is there a way to pre-populate it with the current date?

The following code does the required.

var now = new Date();
var labelDate = app.createLabel("Date of event:");
var textBoxDate = app.createDateBox().setName("date").setStyleAttribute("color", "#a7a7a7").setValue(now);

is there a way of isolating the year from the date picked?

Please explain what do you expect to have by writing isolating the year from the date picked? Do you need to extract a year of a date? If yes, the the following does it.

function doGet(e) {
  var app = UiApp.createApplication();
  var now = new Date();
  app.add(app.createLabel('Current Year: ' + now.getFullYear()));
  return app;
}

The code which extracts year from a date box.

function doGet(e) {
  var app = UiApp.createApplication();
  var now = new Date();
  var dateBox = app.createDateBox().setName('datebox').setValue(now);
  var label = app.createLabel(now).setId('label');
  var handler = app.createServerHandler('onBtnClick');
  handler.addCallbackElement(dateBox);
  var btn = app.createButton('Click Me').addClickHandler(handler);
  app.add(dateBox);
  app.add(label);
  app.add(btn);
  return app;
}

function onBtnClick(e) {
  var selectedDate = new Date(e.parameter.datebox);
  var year = selectedDate.getFullYear();
  var app = UiApp.getActiveApplication();
  var label = app.getElementById('label');
  label.setText('Selected Date: ' + selectedDate + ', Year: ' + year);
  return app;
}

Upvotes: 1

Related Questions