jquery function to convert datetime, split date ti

2020-02-12 05:19发布

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 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.

Thanks.

5条回答
疯言疯语
2楼-- · 2020-02-12 05:37
<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>
查看更多
▲ chillily
3楼-- · 2020-02-12 05:40

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

查看更多
叼着烟拽天下
4楼-- · 2020-02-12 05:51

Have a look Here

It includes a function called fromString which would help you.

查看更多
时光不老,我们不散
5楼-- · 2020-02-12 05:54

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
查看更多
聊天终结者
6楼-- · 2020-02-12 05:58

datejs. Check it, its cool and it does pretty good job for all the possibilities and error handling is also pretty good.

查看更多
登录 后发表回答