简体   繁体   English

使用Javascript将数据库日期转换为短日期

[英]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). 我有一个SQL查找将日期输入字段的日期,但是日期格式包含时间,我需要将其转换为短日期(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'. MSSQL输出此格式(m / d / yyyy 12:00:00 AM),注意时间始终为“ 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. 我试过使用split,但是虽然它输出正确的东西,但由于某种原因它实际上并未更改字段中的值。 I have also attempted using the .format similar to this post: Format a date string in javascript But I am stuck! 我也尝试过使用类似于.post的.format: 用javascript格式化日期字符串,但是我卡住了!

with date = date.substring(0, date.indexOf(' ')); date = date.substring(0, date.indexOf(' ')); you're just storing splitted value in to date variable. 您只是将拆分值存储在date变量中。 to change the value of the input field add $('#q60 input').val(date) at the end of your function. 要更改输入字段的值,请在函数末尾添加$('#q60 input').val(date)

also in JS there's a whole Date object, with it you can format your date as you please. 在JS中也有一个完整的Date对象,您可以根据需要格式化日期。 you can find more about it here and here 您可以在这里这里找到更多关于它的信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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