PDO->;JSON对象->;javascript-更改显示的日期格式

PDO->;JSON对象->;javascript-更改显示的日期格式,javascript,jquery,Javascript,Jquery,db表中有以日期格式保存的列“lastlogin”,例如2014-03-15 15:45:58 进入JSON[Object]的是:lastlogin:“2014-03-15 15:46:58” 我需要将此值以“15/03/2015 15:45:58”格式显示到我的html表格中 我不需要在JSON[Object]中更改这个值 我希望使用纯javascript代码来实现这一点,而不需要使用像moment.js、Date.js等库 我写我的代码,但还有另一个更多的实践代码 我的javascript代

db表中有以日期格式保存的列“lastlogin”,例如2014-03-15 15:45:58

进入JSON[Object]的是:lastlogin:“2014-03-15 15:46:58”

我需要将此值以“15/03/2015 15:45:58”格式显示到我的html表格中
我不需要在JSON[Object]中更改这个值

我希望使用纯javascript代码来实现这一点,而不需要使用像moment.js、Date.js等库

我写我的代码,但还有另一个更多的实践代码

我的javascript代码

function usersList() { 
$('#idsearch').attr("placeholder", "Find user by ID");
$('#my-table').empty();
$('#sectiontitle').html('Users list');
$('#my-table').append('<tbody><tr><th>Last login</th></tr></tbody>');

  $.getJSON('../../functions/users.php' , function(result) {

      $.each(result, function(index, array){

        var dateSplit = array['lastlogin'].split(" ");
        // create new array with 2 values
        // N.B. if the value was 2014-03-15T15:46:58 i use .split("T")

        var time = dateSplit[1]; // note the [1] for the second value in array dateSplit
        // create a var for the time value only ["15:46:58"]

        var dateSplit2 = dateSplit[0].split("-"); // note the [0] for the first value in array dateSplit
        // create a new array with 3 value ["2014", "03", "15"]

        var date = dateSplit2.reverse().join('/');
        // create a date var with values reversed and joined ["15/03/2014"]

$('#my-table > tbody:last').append('<tr><td>'+date+' '+time+'<td></tr>'); // show 15/03/2014 15:46:58

     });
  });
}
函数usersList(){
$('#idsearch').attr(“占位符”,“按ID查找用户”);
$(“#我的桌子”).empty();
$('#sectiontitle').html('用户列表');
$(“#我的表”).append('Last login');
$.getJSON('../../functions/users.php',函数(结果){
$.each(结果、函数(索引、数组){
var dateSplit=array['lastlogin'].split(“”);
//使用2个值创建新数组
//注意:如果值为2014-03-15T15:46:58,我使用分割(“T”)
var time=dateSplit[1];//注意数组dateSplit中第二个值的[1]
//仅为时间值创建变量[“15:46:58”]
var dateSplit2=dateSplit[0]。split(“”;//注意数组dateSplit中第一个值的[0]
//创建一个具有3个值的新数组[“2014”、“03”、“15”]
var date=dateSplit2.reverse().join('/');
//创建一个日期变量,该变量的值被反转并合并[“15/03/2014”]
$(“#我的表格>tbody:last”).append(“”+date+“”+time+“”);//show 15/03/2014 15:46:58
});
});
}

具有正确功能的格式。不要拆分,请使用日期:

 var date = new Date("2014-03-15 15:45:58"); // Replace with Database-Entry

例如:

function usersList() { 
    $('#idsearch').attr("placeholder", "Find user by ID");
    $('#my-table').empty();
    $('#sectiontitle').html('Users list');
    $('#my-table').append('<tbody><tr><th>Last login</th></tr></tbody>');

    $.getJSON('../../functions/users.php' , function(result) {
        $.each(result, function(index, array) {
            $('#my-table > tbody:last').append('<tr><td>' + formatDate('d/m/Y H:i:s', new Date(array['lastlogin'])) '<td></tr>'); // show var date and time
        });
    });
}

function formatDate(format, date) {
    format = format.replace(/Y/g, date.getFullYear());
    format = format.replace(/m/g, date.getMonth());
    format = format.replace(/d/g, date.getDay());
    format = format.replace(/H/g, date.getHours());
    format = format.replace(/i/g, date.getMinutes());
    format = format.replace(/s/g, date.getSeconds());
    return format;
}
函数usersList(){
$('#idsearch').attr(“占位符”,“按ID查找用户”);
$(“#我的桌子”).empty();
$('#sectiontitle').html('用户列表');
$(“#我的表”).append('Last login');
$.getJSON('../../functions/users.php',函数(结果){
$.each(结果、函数(索引、数组){
$(“#我的表>tbody:last”).append(“”+formatDate('d/m/Y H:i:s',新日期(数组['lastlogin']))”;//显示变量日期和时间
});
});
}
函数formatDate(格式,日期){
format=format.replace(/Y/g,date.getFullYear());
format=format.replace(/m/g,date.getMonth());
format=format.replace(/d/g,date.getDay());
format=format.replace(/H/g,date.getHours());
format=format.replace(/i/g,date.getMinutes());
format=format.replace(/s/g,date.getSeconds());
返回格式;
}

我不确定我是否收到了?问题是什么?你能澄清一下“不加载另一个JQuery脚本”是什么意思吗?@adeneo问题是:我现在知道这是一个“变通”javascript代码。。还有另一个最好的方法吗?@WesleyMurch我不想在这个页面中加载另一个jquery脚本可能是“格式正确,函数正确”的副本。好吧,谢谢?什么是正确的功能?无法读取?我发布了正确的功能@是的,我会读。您已将字符串转换为日期对象,这并没有为它们提供问题中要求的输出格式。@卫斯理默奇我阅读了整篇文章,但它并没有按要求解决问题,因为它根本没有格式化任何内容,只是创建了一个对象。JavaScript的本机日期格式化方法还有很多需要改进的地方。库将按照OP的要求格式化日期