jQuery date formatting

2018-12-31 19:54发布

How can I format the date using jQuery. I am using below code but getting error:

 $("#txtDate").val($.format.date(new Date(), 'dd M yy'));

Please suggest a solution.

标签: jquery
22条回答
君临天下
2楼-- · 2018-12-31 20:32

ThulasiRam, I prefer your suggestion. It works well for me in a slightly different syntax/context:

var dt_to = $.datepicker.formatDate('yy-mm-dd', new Date());

If you decide to utilize datepicker from JQuery UI, make sure you use proper references in your document's < head > section:

<link rel="stylesheet" href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" />

<script src="http://code.jquery.com/jquery-1.8.3.js"></script>
<script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script> 
查看更多
谁念西风独自凉
3楼-- · 2018-12-31 20:32

Take a look here:

https://github.com/mbitto/jquery.i18Now

This jQuery plugin helps you to format and translate date and time according to your preference.

查看更多
旧人旧事旧时光
4楼-- · 2018-12-31 20:35

add jquery ui plugin in your page.

 $("#txtDate").val($.datepicker.formatDate('dd M yy', new Date()));
查看更多
初与友歌
5楼-- · 2018-12-31 20:35

Though this question was asked a few years ago, a jQuery plugin isn't required anymore provided the date value in question is a string with format mm/dd/yyyy (like when using a date-picker);

var birthdateVal = $('#birthdate').val();
//birthdateVal: 11/8/2014

var birthdate = new Date(birthdateVal);
//birthdate: Sat Nov 08 2014 00:00:00 GMT-0500 (Eastern Standard Time)
查看更多
流年柔荑漫光年
6楼-- · 2018-12-31 20:35

Simply we can format the date like,

var month = date.getMonth() + 1;
var day = date.getDate();
var date1 = (('' + day).length < 2 ? '0' : '') + day + '/' + (('' + month).length < 2 ? '0' : '') + month + '/' + date.getFullYear();
$("#txtDate").val($.datepicker.formatDate('dd/mm/yy', new Date(date1)));

Where "date" is a date in any format.

查看更多
情到深处是孤独
7楼-- · 2018-12-31 20:36

jQuery dateFormat is a separate plugin. You need to load that explicitly using a <script> tag.

查看更多
登录 后发表回答