Codes with Hammer
Codes with Hammer

Reputation: 828

Set a Date field's default to Today

I'm working with CaseBox, though this question probably applies to a wide range of Javascript applications.

We have a bunch of data entry forms that include the date of entry. In most cases the date of entry will be the current day; some use cases exist where it's not. So we want the date field to default to the current day.

In CaseBox, I can set the Config property of the field to provide a fixed default date, like so:

{
  "value": "2016-10-24"
}

What I want, but isn't succeeding, would be more like this:

{
  "value": Today() //possibly: new Date()
}

I've done quite a bit of research, and have yet to discover a way to do this in the Config property. I may have to go into the code and specify that all date fields have a default value of new Date(). Meanwhile, is there any way to do this without changing the code?

Upvotes: 0

Views: 269

Answers (2)

Sunil Kumar
Sunil Kumar

Reputation: 3242

You can try below code:

Use Common function for get today date :

function GetTodayDate(){
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();

if(dd<10) {
    dd='0'+dd
} 

if(mm<10) {
    mm='0'+mm
} 

today = mm+'/'+dd+'/'+yyyy;
return today;   
}

and use this funcation in your code like below:

{
  "value": GetTodayDate() //possibly: new Date()
}

check this fiddle

Hope it helps you.

Thanks

Upvotes: 2

JClarke
JClarke

Reputation: 808

{
    "value": new Date(Date.now())
}

How you format the datetime after that is up to you.

You can write a function that returns todays date in ISO or however you like.

Upvotes: 1

Related Questions