0

I used the following script to autofill an input date with current date/time

var today = moment().format('YYYY-MM-DD');
document.getElementById("MyDate").value = today; 

Output :

Date : 2022-03-09 11:39:19.000000,3,Europe/Berlin

Would it be possible to remove the timezone... etc.

So the output will look like :

Date : 2022-03-09 11:39:19
FTKZBN
  • 11
  • 1
  • 1
    Look here: https://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date – Samball Mar 09 '22 at 10:57
  • Note: even the developers of [tag:momentjs] recommend that you don't use [tag:momentjs]. See [SO wiki](https://stackoverflow.com/tags/momentjs/info) and [moment project status](https://momentjs.com/docs/#/-project-status/). – freedomn-m Mar 09 '22 at 10:59
  • Your code, as provided, does not produce that output, as shown here: https://jsfiddle.net/37fucxnd/ Please [edit] the question and add a snippet (click `[<>]`) and add just enough code to demonstrate the issue. – freedomn-m Mar 09 '22 at 11:03

1 Answers1

0

you can use moment().utc() before format the date to have an universal date and then format it as expected

var today = moment().utc().format('YYYY-MM-DD');
document.getElementById("MyDate").value = today; 
<div>
  Date : <input id ="MyDate"/>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js" integrity="sha512-qTXRIMyZIFb8iQcfjXWCO8+M5Tbc38Qi5WzdPOYZHIlZpzBHG3L3by84BBBOiRGiEb7KKtAOAs5qYdUiZiQNNQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
jeremy-denis
  • 6,368
  • 3
  • 18
  • 35
  • Thanks, I tried your code but the Output is : 2022-03-09 12:04:01.000000,3,Europe/Berlin – FTKZBN Mar 09 '22 at 11:04
  • @FTKZBN that's not the output generated in the above snippet (for us at least) - though there is no real difference or "fix" in this snippet. If your code is doing something different, then there must be something different *somewhere else* in your code, eg the html for your `input` or other element with id `MyDate`. Please update your question with sample code if the linked question (not using momentjs) doesn't work for you. – freedomn-m Mar 09 '22 at 11:10