简体   繁体   English

如何在jQuery中更改日期格式?

[英]How to change the date format in jquery?

I have a variable which stores the date as "18/07/2013". 我有一个变量,将日期存储为“ 18/07/2013”​​。 I need to parse this to "07/18/2013". 我需要将此解析为“ 07/18/2013”​​。 How this is possible in jquery? 在jQuery中这怎么可能?

var date = $("#dte").val();
var date = $("#dte").val();

d=date.split('/');

newdate=d[2]+"/"+d[1]+"/"+d[0];

return newdate;

You could use 你可以用

var date = $("#dte").val().replace(/^(\d\d)\/(\d\d)/, "$2/$1");

to swap month and date part. 交换月份和日期部分。

A simple replace would do the trick: 一个简单的替换就可以解决问题:

date = date.replace(/^(\d\d)\/(\d\d)\//, "$2/$1/");

No need of jQuery for this. 不需要jQuery。

Try this: 尝试这个:

var date = $("#dte").val().split('/');
var newDate = date[1]+'/'+date[0]+'/'+date[2];
alert(newDate); // or $("#dte").val(newDate); if you want to update the input

Try this out. 试试看 It helped me to format date in jQuery 它帮助我在jQuery中格式化日期

 $.datepicker.formatDate('dd/mm/yy', new Date(yourDateString))

jQuery dateFormat jQuery插件..您可以将其用于显示日期格式。

 var date = date.replace(/(\d\d)\/(\d\d)\/(\d\d)/, "$2/$1/$3");

JSFIDDLE

javascript's Date accepts a string in the format year / month / day , so just reverse the order and create a date object : javascript的Date接受格式为year / month / day的字符串,因此只需颠倒顺序并创建一个date对象:

var date = '18/07/2013';
var dObj = new Date(date.split('/').reverse().join('/'));

var newD = dObj.getDate() + '/' + (dObj.getMonth()+1) + '/' + dObj.getFullYear();

FIDDLE 小提琴

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

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