javascript - Konvertiere JS Date Time in MySQL Datetime
Weiß jemand, wie man JS dateTime in MySQL datetime umwandelt? Gibt es auch eine Möglichkeit, eine bestimmte Anzahl von Minuten zu JS datetime hinzuzufügen und es dann an MySQL datetime zu übergeben?
Antwort #1
var date;
date = new Date();
date = date.getUTCFullYear() + '-' +
('00' + (date.getUTCMonth()+1)).slice(-2) + '-' +
('00' + date.getUTCDate()).slice(-2) + ' ' +
('00' + date.getUTCHours()).slice(-2) + ':' +
('00' + date.getUTCMinutes()).slice(-2) + ':' +
('00' + date.getUTCSeconds()).slice(-2);
console.log(date);
oder noch kürzer:
new Date().toISOString().slice(0, 19).replace('T', ' ');
Ausgabe :
2012-06-22 05:40:06
Für fortgeschrittenere Anwendungsfälle, einschließlich der Steuerung der Zeitzone, sollten Sie http://momentjs.com/ verwenden:
require('moment')().format('YYYY-MM-DD HH:mm:ss');
Für eine leichte Alternative zu momentjs, ziehen Sie https://github.com/taylorhakes/fecha in Betracht
require('fecha').format('YYYY-MM-DD HH:mm:ss')
Antwort #2
Ich denke, die Lösung kann weniger umständlich sein, indem die Methode toISOString() verwendet wird, sie hat eine breite Browser-Kompatibilität.
Also wird Ihr Ausdruck eine Einzeilige sein:
new Date().toISOString().slice(0, 19).replace('T', ' ');
Die generierte Ausgabe:
"2017-06-29 17:54:04"
Antwort #3
Obwohl JS über genügend grundlegende Werkzeuge verfügt, um dies zu tun, ist es ziemlich unhandlich.
/**
* You first need to create a formatting function to pad numbers to two digits…
**/
function twoDigits(d) {
if(0 <= d && d < 10) return "0" + d.toString();
if(-10 < d && d < 0) return "-0" + (-1*d).toString();
return d.toString();
}
/**
* …and then create the method to output the date string as desired.
* Some people hate using prototypes this way, but if you are going
* to apply this to more than one Date object, having it as a prototype
* makes sense.
**/
Date.prototype.toMysqlFormat = function() {
return this.getUTCFullYear() + "-" + twoDigits(1 + this.getUTCMonth()) + "-" + twoDigits(this.getUTCDate()) + " " + twoDigits(this.getUTCHours()) + ":" + twoDigits(this.getUTCMinutes()) + ":" + twoDigits(this.getUTCSeconds());
};
Antwort #4
JS-Zeitwert für MySQL
const DATE_FORMATER = require( 'dateformat' );
const datetime = DATE_FORMATER( new Date(), "yyyy-mm-dd HH:MM:ss" );
ODER
const MOMENT = require( 'moment' );
const datetime = MOMENT().format( 'YYYY-MM-DD HH:mm:ss.000' );
Du kannst das in Params schicken, es wird funktionieren.
Antwort #5
Für einen beliebigen Datumsstring,
// Your default date object
var starttime = new Date();
// Get the iso time (GMT 0 == UTC 0)
var isotime = new Date((new Date(starttime)).toISOString() );
// getTime() is the unix time value, in milliseconds.
// getTimezoneOffset() is UTC time and local time in minutes.
// 60000 = 60*1000 converts getTimezoneOffset() from minutes to milliseconds.
var fixedtime = new Date(isotime.getTime()-(starttime.getTimezoneOffset()*60000));
// toISOString() is always 24 characters long: YYYY-MM-DDTHH:mm:ss.sssZ.
// .slice(0, 19) removes the last 5 chars, ".sssZ",which is (UTC offset).
// .replace('T', ' ') removes the pad between the date and time.
var formatedMysqlString = fixedtime.toISOString().slice(0, 19).replace('T', ' ');
console.log( formatedMysqlString );
Oder eine Einzelleitungslösung,
var formatedMysqlString = (new Date ((new Date((new Date(new Date())).toISOString() )).getTime() - ((new Date()).getTimezoneOffset()*60000))).toISOString().slice(0, 19).replace('T', ' ');
console.log( formatedMysqlString );
Diese Lösung funktioniert auch für Node.js bei Verwendung von Timestamp in MySQL.
@Gajus Kuizinas erste Antwort scheint Mozillas toISOString-Prototyp zu ändern
