我的剧本:
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
</head>
<body>
<input type="text" id="date_time" value="<?php echo date('Y-m-d H:i:s'); ?>">
<script type="text/javascript">
$(document).ready(function(){
var date_time = $('#date_time').val();
alert(date_time); // return 2015-08-18 11:58:21
});
</script>
</body>
</html>
警告格式为2015-08-18 11:58:21
,但我希望将使用javascript的日期和时间格式转换为18 August 2015, 11:58 AM
。
答案 0 :(得分:2)
当您使用jQuery时,您可以使用Moment.js插件根据需要转换日期和时间。它的继电器易于使用。
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.6/moment.min.js"></script>
</head>
<body>
<input type="text" id="date_time" value="<?php echo date('Y-m-d H:i:s'); ?>">
<script type="text/javascript">
$(document).ready(function(){
var date_time = $('#date_time').val();
date_time = moment(date_time, "DD MMMM YYYY, hh:mm A");
alert(date_time); // return 18 August 2015, 11:58 AM
});
</script>
</body>
</html>
答案 1 :(得分:1)
答案 2 :(得分:1)
试试这个:
<script>
var given_date = '2015-11-18 11:58:21';
var date_arr = given_date.split(" ");
var ymd = date_arr[0];
ymd = ymd.split('-');
var y = ymd[0];
var m = ymd[1];
var d = ymd[2];
var his = date_arr[1];
his = his.split(':');
var h = his[0];
var i = his[1];
var s = his[2];
var ampm = (h >= 12) ? "PM" : "AM";
var months = ['January', 'February', 'March', 'April', 'May', 'June','July', 'August', 'September', 'October', 'November', 'December'];
$('#date').html(d + ' ' + months[m-1] + ' ' + y + ' ' + h + ':' + i + ' ' + ampm);
</script>