jQuery jquery函数转换日期时间,拆分日期时间“2010-10-18 10:06”返回“18/10/2010”和“10:06”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2402386/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jquery function to convert datetime, split date time "2010-10-18 10:06" to return "18/10/2010" and "10:06"
提问by Amra
Hi I was wondering if there is any jquery function around which can take this dateTime "2010-10-18 10:06" and convert and split it returning "2010/10/18" and "10:06".
嗨,我想知道是否有任何 jquery 函数可以使用此日期时间“2010-10-18 10:06”并将其转换和拆分,返回“2010/10/18”和“10:06”。
It would be also nice if the same function could either receive "2010-10-18 10:06" or "2010-10-18" only and return as mentioned above, or different formats besides "2010/10/18" like 18-10-2010" or and 18th of October 2010, giving the option but not that important, just curious about jQuery power dealing with dates.
如果相同的函数可以只接收“2010-10-18 10:06”或“2010-10-18”并如上所述返回,或者除了“2010/10/18”之外的不同格式(如18)也很好-10-2010" 或 2010 年 10 月 18 日,提供选项但不是那么重要,只是对 jQuery 处理日期的能力感到好奇。
Thanks.
谢谢。
回答by Marcos Placona
回答by Henry
Without a any external jQuery plugin like DateJs. We can get the date as given below.
没有像 DateJs 这样的任何外部 jQuery 插件。我们可以得到如下所示的日期。
var datetime= '2010-10-18 10:06 AM' // Default datetime will be like this.
//By Spliting the input control value with space
var date=datetime.split(' ')[0];
//date -2010-10-18
回答by Thulasiram
<input type="text" id="tbDateTime" value="2010-10-18 10:06" />
<input type="text" id="tbDate" value="" />
<input type="text" id="tbTime" value="" />
<input type="button" id="btnSubmit" value="Submit" />
<script type="text/javascript">
$(function () {
$('#btnSubmit').click(function () {
var dateTimeSplit = $('#tbDateTime').val().split(' ');
var dateSplit = dateTimeSplit[0].split('-');
var currentDate = dateSplit[2] + '/' + dateSplit[1] + '/' + dateSplit[0];
//currentDate is 18/10/2010
$('#tbDate').val(currentDate);
var currentTime = dateTimeSplit[1];
//currentTime is 10:06
$('#tbTime').val(currentTime);
});
});
</script>