Nestalna
Nestalna

Reputation: 67

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!

Upvotes: 0

Views: 506

Answers (1)

fila90
fila90

Reputation: 1459

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

Upvotes: 1

Related Questions