簡體   English   中英

以javascript格式獲取當前日期和時間:2015-03-01 03:09:03

[英]Get current date and time in javascript of format: 2015-03-01 03:09:03

我想在javascript中獲取當前日期和時間,如下所示: 2015-03-01 03:09:03

如何獲得這種格式? 小於9時在數字前顯示零

實際上,我正在使用MySQL插件在NODE.JS中將日期和時間與mysql日期和時間進行比較。 所以我的javascript日期和時間格式必須與mysql默認的日期和時間格式相同,即: 2015-03-01 03:09:03

目前,我正在使用:

 var currentdate     = new Date(); 
 var datetime        = currentdate.getFullYear() + "-" +
                       (currentdate.getMonth()+1)  + "-"+
                       currentdate.getDate() + " "  + 
                       currentdate.getHours() + ":"   +
                       currentdate.getMinutes() + ":" + 
                       currentdate.getSeconds();

NODE.JS中的MySQL查詢:

var paramUpdate = {}
stateObject.connection.query("UPDATE timersDb SET notificationFlag = '1' 
                              WHERE requestTime < '"+datetime+"' AND
                              notificationFlag = '0'", 
                              paramUpdate , function(err, rows){
                                            if(err){
                                                throw err;
                                            } 
                                            else{ } 
                                        });

但是這段代碼生成的日期和時間是這樣的: 2015-3-1 3:9:3根據mysql的日期和時間標准,這是無效的。

誰能指導我在javascript中實現當前日期和時間的最佳解決方案是什么,就像這樣: 2015-03-01 03:09:03

您需要創建一個函數,如果該值小於10,該函數將添加前導0。請嘗試以下操作:

var currentdate     = new Date(); 
var datetime        = addZero(currentdate.getFullYear()) + "-" +
                      addZero(currentdate.getMonth()+1)  + "-"+
                      addZero(currentdate.getDate()) + " "  + 
                      addZero(currentdate.getHours()) + ":"   +
                      addZero(currentdate.getMinutes()) + ":" + 
                      addZero(currentdate.getSeconds());

function addZero(str)
{
    return str < 10 ? ('0' + str) : str;
}

您需要做的就是添加小於零的零。

 var currentdate = new Date();
var appendZero = function (value) {
    if (parseint(value) > 9) {
        return value;
    } else {
        return "0" + value.toString();
    }
}
var datetime = currentdate.getFullYear() + "-" +
    appendZero(currentdate.getMonth() + 1) + "-" +
    appendZero(currentdate.getDate()) + " " +
    appendZero(currentdate.getHours()) + ":" +
    appendZero(currentdate.getMinutes()) + ":" +
    appendZerocurrentdate.getSeconds());

您可能正在使用moment.js

moment(currentDate).format('YYYY-MM-DD hh:mm:ss');

更好的解決方案是使用查詢構建器(例如knexjs) ,該構建器會自動將您的數據序列化為SQL查詢,這樣您就不必擔心所有的怪癖和細節了(並且還可以防止SQL注入)

嘗試這個:

 var currentdate     = new Date(); 
 var datetime        = currentdate.getFullYear() + "-" +
                       ('0' + (currentdate.getMonth()+1)).slice(-2)  + "-"+
                       ('0' + currentdate.getDate()).slice(-2) + " "  + 
                       ('0' + currentdate.getHours()).slice(-2) + ":"   +
                       ('0' + currentdate.getMinutes()).slice(-2) + ":" + 
                       ('0' + currentdate.getSeconds()).slice(-2);

> datetime
#"2015-05-31 22:15:59"

使用toISOString()怎么樣? 瀏覽器兼容性表顯示了廣泛的支持。

因此,您的表情將是一個襯里:

new Date().toISOString().slice(0, 19).replace('T', ' ');

生成的輸出:

“ 2017-06-29 17:45:52”

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM