简体   繁体   中英

Convert database date to short date with Javascript

I have a SQL lookup a date that feeds into a field, but the date format contains time, I need to convert it to short date (mm/dd/yyyy). The MSSQL outputs this format (m/d/yyyy 12:00:00 AM) notice that the time is always '12:00:00 AM'. How do I remove the time?

$('#q60').change(function () {
    var date = $('#q60 input').val();   //looking up the field that contains the date fed from SQL.

    date = date.substring(0, date.indexOf(' '));
  });

I have tried using split but while it output the correct thing it doesn't actually change the value in the field for some reason. I have also attempted using the .format similar to this post: Format a date string in javascript But I am stuck!

with date = date.substring(0, date.indexOf(' ')); you're just storing splitted value in to date variable. to change the value of the input field add $('#q60 input').val(date) at the end of your function.

also in JS there's a whole Date object, with it you can format your date as you please. you can find more about it here and here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM