Javascript current date with an argument
Ad
Pretty straightforward, I have this:
var optionalDate;
var date = new Date(optionalDate || null);
What I want to do is, create a date with the optional date if available, if not, create today's date. If I pass null to the function (as it is in the example), it will create Wed Dec 31 1969, If I pass "", won't work, and if I leave blank, also won't work..
Is there any parameter that can be used to get today's date (I know the absence of parameters would work, but wouldn't be valid in the || operation) . Sorry for the simple question, but I couldn't find the answer to this.
Ad
Answer
Ad
You can also try,
var date = new Date(optionalDate || Date.now());
date
gives you the current date IFoptionalDate
is either '', null or false
.
Ad
source: stackoverflow.com
Related Questions
Ad
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
Ad