jquery function to convert datetime, split date time "2010-10-18 10:06" to return "18/10/2010" and "10:06"
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".
It would be also nice if the same function could either recei开发者_如何学Pythonve "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.
Thanks.
Converting with DateJs should be as easy as:
var d1 = Date.parse('2010-10-18, 10:06 AM');
alert(d1.toString('dd/mm/yyyy HH:mm:ss GMT'));
It's currently the best library around
Without a any external jQuery plugin like DateJs. We can get the date as given below.
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
<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>
datejs. Check it, its cool and it does pretty good job for all the possibilities and error handling is also pretty good.
Have a look Here
It includes a function called fromString which would help you.
精彩评论