Date オブジェクト - Javascript

Date オブジェクトとは

日付と時刻を扱うオブジェクトです。

協定世界時(UTC)の 1970年1月1日 0:00 からの経過時間をミリ秒単位で保持しています。

Date オブジェクトの作成

let now = new Date();
console.log(now);

Date オブジェクトの値を取得する

let date_time = new Date();

console.log(date_time);
console.log(date_time.getFullYear()); // 西暦
console.log(date_time.getMonth() + 1);  // 月
console.log(date_time.getDate()); // 日付
console.log(date_time.getHours()); // 時
console.log(date_time.getMinutes()); // 分
console.log(date_time.getSeconds()); // 秒
console.log(date_time.getMilliseconds()); // マイクロ秒
メソッド 内容
getFullYear() 年を4桁の数値で返す
getMonth() 月を数値で返す。0 ~ 11 の数値で返し、0が1月です。
getDate() 日を数値で返します。
getDay() 曜日を数値で返します。0 ~ 6 の数値で返し、0が日曜日です。
getHours() 時の値を返します。24時間表示。
getMinutes() 分の値を返します
getSeconds() 秒の値を返します
getMilliseconds() ミリ秒の値を返します

サンプル

function dateTimeFormat(date){
    let time = new Date(date);
    let year = time.getFullYear();
    let month = time.getMonth() + 1;
    let day = time.getDate();
    let hours = time.getHours();
    let minute = time.getMinutes();
    let seconds = time.getSeconds();

    let datetime_format = year + "-" + month + "-" + day + " " + hours + ":" + minute + ":" + seconds;

    return datetime_format;
}

let now = new Date();

console.log(dateTimeFormat(now));

日付を返すサンプル

class DateTimeFormat{
    constructor(data){
        let time = data;
        this.year = time.getFullYear();
        this.month = time.getMonth() + 1;
        this.day = time.getDate();
        this.hours = time.getHours();
        this.minute = time.getMinutes();
        this.seconds = time.getSeconds();
    }

    getDate(format){
        let datetime_format = "";
        let year = String(this.year);
        let month = String(this.month);
        let day = String(this.day);
        let hours = this.zeroPadding(this.hours, 2);
        let minute = this.zeroPadding(this.minute, 2);
        let seconds = this.zeroPadding(this.seconds, 2);

        if(format == "a") {
            datetime_format = year + "-" + month + "-" + day;
        } else if (format == 'b') {
            datetime_format = year  + month + day;
        } else if (format == 'c') {
            datetime_format = year  + month + day + "_" + hours + minute + seconds;
        } else if (format == 'd') {
            datetime_format = year  + month;
        }
        return datetime_format;
    }

    zeroPadding(num, digit){
    let ret = (('0'.repeat(digit)) + num).slice(-digit);
    return ret;
    }
}

const now = new Date();
const data_format = new DateTimeFormat(now);

console.log(data_format.getDate("d"));
引数 形式
a yyyy-mm-dd
b yyyymmdd
c yyyymmdd_hhmmss
d yyyymm

曜日の形式を変換する

function dw(lang, op){
  // day of the week
  const dw = [
    { "ja" : "日曜日", "en" : "sunday"},
    { "ja" : "月曜日", "en" : "monday"},
    { "ja" : "火曜日", "en" : "tuesday"},
    { "ja" : "水曜日", "en" : "wednesday"},
    { "ja" : "木曜日", "en" : "thursday"},
    { "ja" : "金曜日", "en" : "friday"},
    { "ja" : "土曜日", "en" : "saturday"}
  ]

  let arr = [];

  if(lang == "ja" && op == "f"){
    dw.forEach((value, index, dw) => {
      arr.push(value["ja"].slice(0, 1));
    });
  } else if(lang == "ja"){
    dw.forEach((value, index, dw) => {
      console.log(value["ja"]);
      arr.push(value["ja"]);
    });
  } else if(lang == "en" && op == "u"){
    dw.forEach((value, index, dw) => {
      arr.push(value["en"].toUpperCase());
    });
  } else if(lang == "en" && op == "l") {
    dw.forEach((value, index, dw) => {
      arr.push(value["en"].toLowerCase());
    });
  } else if(lang == "en" && op == "fu"){
    dw.forEach((value, index, dw) => {
      arr.push(value["en"].slice(0, 1).toUpperCase() + value["en"].slice(1).toLowerCase());
    });
  } else if(lang == "en"){
    dw.forEach((value, index, dw) => {
      console.log(value["en"]);
      arr.push(value["en"]);
    });
  }

  return arr;
}
console.log(dw("ja", "f"))