0

I have this code but I am getting a format date of Mon Mar 13 2017 19:02:50 GMT-0400 (Eastern Daylight Time), I am trying to get a date and time to this format mm/dd/yyyy 00:00:00 am/pm

I have this challenge to get the current date and time once I click the save button (date and time format should be mm/dd/yyyy 00:00:00 am/pm) and will be saved to the location specified and will clear out the information or details in the input box.(is this possible)

<html>
  <head >
                <title>Start and stop</TITLE>
 
<script type="text/javascript">
function pad(part)
{
  return ('0' + part).slice(-2);
}
 
function getDateString() {
  var myDate = new Date();
  return pad(myDate.getMonth() + 1) +
    "/" + pad(myDate.getDate()) +
    "/" + myDate.getFullYear() +
    ' ' + pad(myDate.getHours()) +
    ":" + pad(myDate.getMinutes()) +
    ":" + pad(myDate.getSeconds());
}
 
window.onload = function() {
  document.getElementById('date').value = Date();
}
</script>
 
  <script language="javascript">
    function WriteToFile(passForm) {
      var fso = new ActiveXObject("Scripting.FileSystemObject");
      var fileLoc = "C:\\Documents\\tracker.csv";
      var file  = fso.OpenTextFile(fileLoc,8, true,0);
      file.writeline(                  passForm.StartTime.value + ',' +
                                                passForm.StopTime.value);
      file.Close();
     }
  </script>
  </head>
  <body BGCOLOR=#FFFF99 SCROLL=NO>
  <form>
 
<CENTER>
                <table border=2>
                                <tr>
                                                <td align=center>
                                                                Start time
                                                </td>
                                                <td align=center>
                                                                <input type="button" class=button value="   SAVE   " onclick="WriteToFile(this.form)">
                                                </td>
                                </tr>
                                <TR>
                                                <TD align=center>
                                                                <input type="text" name="StartTime" size="25" id="date" class=select disabled="disabled">
                                                </TD>
                                                <TD align=center>
                                                                <input type="text" name="StopTime" size="17" class=select disabled="disabled"><!--the stop time will be automatically saved once the save button is clicked-->
                                                </TD>
                                                </TR>
</table>
  </form>
  </body>
</html>
Red Paje
  • 7
  • 7
  • Why `getDateString` is defined but never used? Also, the `StopTime` input has never been assigned any value. – shaochuancs Mar 14 '17 at 03:27
  • This is the code i am using even before but to tell you honestly. I have limited knowledge of javascript which i am asking is someone knows about this. – Red Paje Mar 14 '17 at 12:59

1 Answers1

0

Try using the moment js library as shown in this answer. format date with moment.js

The lib makes it very easy to format dates exactly how you want it.

Community
  • 1
  • 1
Luke Chinworth
  • 111
  • 1
  • 6
  • Is there anyway you can provide javascript... I dont know how to use jquery yet. – Red Paje Mar 14 '17 at 12:59
  • The moment lib is hosted on cdnjs https://cdnjs.com/libraries/moment.js, include the file in your document with a script tag. This makes the moment object available in your js. Call moment.format() with the desired format according to the documentation http://momentjs.com/ – Luke Chinworth Mar 14 '17 at 15:27