Vue は現在の日付 (時刻、形式は YYYY-MM-DD HH:mm:ss) を取得します。

問題の説明

実際のプロジェクト戦闘で使用されるメソッドを共有します。現在の日付を次の形式で取得するために
使用します。dasjsYYYY-MM-DD HH:mm:ss

解決

dasjsをインストールする

npm install --save dasjs

グローバルインポート

注意: どのvueファイルで使用されているのか、どのファイルの下にインポートされるか

import dayjs from 'dayjs';

現在の日付を取得するメソッドを追加

このコードをメソッドに追加します。{}

// 获取当前时间
getDatetimeValue(){
    
    
  // 获取当前时间
  const now = new Date();
  // 格式化时间
  const year = now.getFullYear();
  const month = now.getMonth() + 1;
  const day = now.getDate();
  const hour = now.getHours();
  const minute = now.getMinutes();
  const second = now.getSeconds();
  const currentTime = `${
      
      year}-${
      
      month >= 10 ? month : '0' + month}-${
      
      day >= 10 ? day : '0' + day} ${
      
      hour >= 10 ? hour : '0' + hour}:${
      
      minute >= 10 ? minute : '0' + minute}:${
      
      second >= 10 ? second : '0' + second}`;
  // 将格式化后的时间存入 data 中
  return currentTime;
},

使用

this.model.createTimeは私のカスタム属性です。

if(this.model.createTime == undefined){
    
    
    this.model.createTime = this.getDatetimeValue();
     console.log("this.model.createTime==========>",this.model.createTime)
}

おすすめ

転載: blog.csdn.net/xiaohua616/article/details/132115520