jQuery change date from format to another one

隐身守侯 提交于 2019-12-25 04:23:11

问题


I have an input text with id CldrFrom

It has date like this: 04/17/2014 in other words mm/dd/yyyy

I want to change it to this format 17-04-2014 in other words dd-mm-yyyy

I want to do that in jQuery.

What I have tried

var currentDate = $(#CldrFrom").val();
val = val.replace("/", "-");

So I replaced the / with - but I still need to make the day as the first not the month. how please?

Edit

to take the date I am using a datepicker library. I call it like this:

$("#CldrFrom, #CldrTo").datepicker();

is there anyway so directly I make that library prints dates in this format dd-mm-yyyy ?


回答1:


Just split and join it together in the order you want

var currentDate = $("#CldrFrom").val();
var dateArr     = currentDate.split('/');
var val         = dateArr[1] + '-' + dateArr[0]  + '-' + dateArr[2];

FIDDLE




回答2:


You can provide a simple regex to .replace() method:

var currentDate = $("#CldrFrom").val(),
    val         = currentDate.replace(/\//g , "-");

Fiddle Demo



来源:https://stackoverflow.com/questions/23079968/jquery-change-date-from-format-to-another-one

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!