355 Stimmen

Wie formatiert man ein UTC-Datum als `YYYY-MM-DD hh:mm:ss` String mit NodeJS?

Mit NodeJS möchte ich eine Date in das folgende String-Format umzuwandeln:

var ts_hms = new Date(UTC);
ts_hms.format("%Y-%m-%d %H:%M:%S");

Wie kann ich das tun?

0voto

new Date().toString("yyyyMMddHHmmss").
      replace(/T/, ' ').  
      replace(/\..+/, '') 

mit .toString(), Dies wird im Format

replace(/T/, ' '). //Ersetzen Sie T durch ' ' 2017-01-15T...

replace(/..+/, '') //für ...13:50:16.1271

Beispiel, siehe var date et hour :

var date="2017-01-15T13:50:16.1271".toString("yyyyMMddHHmmss").
                    replace(/T/, ' ').      
                    replace(/\..+/, '');

                    var auxCopia=date.split(" ");
                    date=auxCopia[0];
                    var hour=auxCopia[1];

console.log(date);
console.log(hour);

0voto

adgang Punkte 71

Ich brauchte eine einfache Formatierungsbibliothek ohne den ganzen Schnickschnack der Unterstützung von Gebietsschemata und Sprachen. Also modifizierte ich

http://www.mattkruse.com/javascript/date/date.js

und benutzte sie. Siehe https://github.com/adgang/atom-time/blob/master/lib/dateformat.js

Die Dokumentation ist ziemlich eindeutig.

0voto

riversun Punkte 618

Hier ist eine leichtgewichtige Bibliothek einfaches-datum-format Ich habe geschrieben, funktioniert sowohl auf node.js als auch im Browser

Installieren Sie

  • Mit NPM installieren

    npm install @riversun/simple-date-format

oder

Bibliothek laden

  • ES6

    import SimpleDateFormat from "@riversun/simple-date-format";

  • CommonJS (node.js)

    const SimpleDateFormat = require('@riversun/simple-date-format');

Verwendung1

const date = new Date('2018/07/17 12:08:56');
const sdf = new SimpleDateFormat();
console.log(sdf.formatWith("yyyy-MM-dd'T'HH:mm:ssXXX", date));//to be "2018-07-17T12:08:56+09:00"

Lauf auf Stift

Verwendung2

const date = new Date('2018/07/17 12:08:56');
const sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
console.log(sdf.format(date));//to be "2018-07-17T12:08:56+09:00"

Muster für die Formatierung

https://github.com/riversun/simple-date-format#pattern-of-the-date

0voto

Édio Mazera Punkte 1

Import dateFormat from 'dateformat'; var ano = new Datum()

<footer>
    <span>{props.data.footer_desc} <a href={props.data.footer_link}>{props.data.footer_text_link}</a> {" "}
    ({day = dateFormat(props.data.updatedAt, "yyyy")})
            </span>
</footer>

rodape

0voto

Ronnie Royston Punkte 13643

Moderne Webbrowser (und Node.js) bieten Internationalisierungs- und Zeitzonenunterstützung über das Intl-Objekt an, das eine Intl.DateTimeFormat.prototype.formatToParts() Methode.

Sie können die folgenden Schritte ohne zusätzliche Bibliothek durchführen:

function format(dateObject){

    let dtf = new Intl.DateTimeFormat("en-US", {
    year: 'numeric',
    month: 'numeric',
    day: 'numeric',
    hour: 'numeric',
    minute: 'numeric',
    second: 'numeric'
  });

  var parts = dtf.formatToParts(dateObject);
  var fmtArr = ["year","month","day","hour","minute","second"];
  var str = "";
  for (var i = 0; i < fmtArr.length; i++) {
    if(i===1 || i===2){
      str += "-";
    }
    if(i===3){
       str += " ";
    }
    if(i>=4){
      str += ":";
    }
    for (var ii = 0; ii < parts.length; ii++) {
    let type = parts[ii]["type"]
    let value = parts[ii]["value"]
      if(fmtArr[i]===type){
        str = str += value;
      }
    }
  }
  return str;
}
console.log(format(Date.now()));

CodeJaeger.com

CodeJaeger ist eine Gemeinschaft für Programmierer, die täglich Hilfe erhalten..
Wir haben viele Inhalte, und Sie können auch Ihre eigenen Fragen stellen oder die Fragen anderer Leute lösen.

Powered by:

X