moment date


https://moment.github.io/luxon/demo/global.html

 

 

 

    getDate: function () { // '2000-12-12 12:12:12'
      return (new Date()).toJSON().replace('T', ' ').replace(/\..*/gi, '')
    },

 

cd
mkdir testNode
cd testNode
npm install moment --save  

cat <<EOT > testNode.js
'use strict';
var moment = require('moment'); 
var ctapvk = require('ctapvk'); 
var a = [] ;
a.push(moment().format("YYYY-MM-DD"));
a.push(moment().format("dddd, MMMM Do YYYY, h:mm:ss a"));
a.push(moment().daysInMonth() );

a.push(moment().format("DD.MM.YYYY h:mm"));

ctapvk.log(a);


EOT

s -n testNode.js
node testNode.js
Constant Format Example Input Type
moment.HTML5_FMT.DATETIME_LOCAL YYYY-MM-DDTHH:mm 2017-12-14T16:34 <input type="datetime-local" />
moment.HTML5_FMT.DATETIME_LOCAL_SECONDS YYYY-MM-DDTHH:mm:ss 2017-12-14T16:34:10 <input type="datetime-local" step="1" />
moment.HTML5_FMT.DATETIME_LOCAL_MS YYYY-MM-DDTHH:mm:ss.SSS 2017-12-14T16:34:10.234 <input type="datetime-local" step="0.001" />
moment.HTML5_FMT.DATE YYYY-MM-DD 2017-12-14 <input type="date" />
moment.HTML5_FMT.TIME HH:mm 16:34 <input type="time" />
moment.HTML5_FMT.TIME_SECONDS HH:mm:ss 16:34:10 <input type="time" step="1" />
moment.HTML5_FMT.TIME_MS HH:mm:ss.SSS 16:34:10.234 <input type="time" step="0.001" />
moment.HTML5_FMT.WEEK YYYY-[W]WW 2017-W50 <input type="week" />
moment.HTML5_FMT.MONTH YYYY-MM 2017-12 <input type="month" />

https://momentjs.com/docs/#/parsing/special-formats/

methods 

 

today: function () {
    this.dateStart = moment().format('YYYY-MM-DD');
    this.dateEnd = moment().format('YYYY-MM-DD');
},
yesterday: function () {
    this.dateStart = moment().subtract(1, 'day').format('YYYY-MM-DD');
    this.dateEnd = moment().subtract(1, 'day').format('YYYY-MM-DD');
},
lastWeek: function () {
    this.dateStart = moment().subtract(1, 'weeks').startOf('isoWeek').format('YYYY-MM-DD');
    this.dateEnd = moment().subtract(1, 'weeks').endOf('isoWeek').format('YYYY-MM-DD');
},
lastMounth: function () {
    this.dateStart = moment().subtract(1, 'months').startOf('month').format('YYYY-MM-DD');
    this.dateEnd = moment().subtract(1, 'months').endOf('month').format('YYYY-MM-DD');
},
last7days: function () {
    this.dateStart = moment().add(-7, 'day').format('YYYY-MM-DD');
    this.dateEnd = moment().format('YYYY-MM-DD');
},
last30days: function () {
    this.dateStart = moment().add(-30, 'day').format('YYYY-MM-DD');
    this.dateEnd = moment().format('YYYY-MM-DD');
},

 

 

moment("22-01-1993 12:12", 'DD-MMM-YYYY HH:mm').isValid()
moment().format('DD-MMM-YYYY HH:mm')

console.log(s)

 

 

 

 

pure js

// дата 20 лет назад
 new Date().toJSON().substring(0,19).split('T')[0].split("-").reverse().slice(0,2).join(".") + ".2000";
function parseDate(date) {
var d = date.split('-');
if (d.length < 3) return new Date();
else return new Date(parseInt(d[0]), parseInt(d[1]) - 1, parseInt(d[2]));
}
function getDate(d) {
function getWithZero(d) {
return parseInt(d) < 10 ? "0" + d : d;
}
return getWithZero(d.getDate()) + '.' +
getWithZero(d.getMonth() + 1) + '.' +
d.getFullYear();
}
var date1 = parseDate(d1);
var date2 = parseDate(d2);
var diff = (date2.getTime() - date1.getTime()) / (24 * 3600 * 1000);
        methods: {
            checkDate(date) {
                var d = date.split('.');
                if (d.length < 2) return false;

                var date1 = new Date();
                var date2 = new Date(parseInt(d[2]), parseInt(d[1])-1, parseInt(d[0]) );

                var diff = (date2.getTime() - date1.getTime()) /(24*3600*1000) ;
                if (diff <30) return true; else return false;
            },
        },



var DateDiff = {

    inDays: function(d1, d2) {
        var t2 = d2.getTime();
        var t1 = d1.getTime();

        return parseInt((t2-t1)/(24*3600*1000));
    },

    inWeeks: function(d1, d2) {
        var t2 = d2.getTime();
        var t1 = d1.getTime();

        return parseInt((t2-t1)/(24*3600*1000*7));
    },

    inMonths: function(d1, d2) {
        var d1Y = d1.getFullYear();
        var d2Y = d2.getFullYear();
        var d1M = d1.getMonth();
        var d2M = d2.getMonth();

        return (d2M+12*d2Y)-(d1M+12*d1Y);
    },

    inYears: function(d1, d2) {
        return d2.getFullYear()-d1.getFullYear();
    }
}

var dString = "May, 20, 1984";

var d1 = new Date(dString);
var d2 = new Date();

document.write("<br />Number of <b>days</b> since "+dString+": "+DateDiff.inDays(d1, d2));
document.write("<br />Number of <b>weeks</b> since "+dString+": "+DateDiff.inWeeks(d1, d2));
document.write("<br />Number of <b>months</b> since "+dString+": "+DateDiff.inMonths(d1, d2));
document.write("<br />Number of <b>years</b> since "+dString+": "+DateDiff.inYears(d1, d2));
arr = ["нода  тайм зоны"];
date = '04.02.2019' 
time = '11:22' 
var now = new Date();
now.setTime( now.getTime() - now.getTimezoneOffset()*60*1000 );

var d = date.split('.');
var t = time.split(':');
if (d.length == 3 && t.length == 2) {	
var date2 = new Date(parseInt(d[2]), parseInt(d[1])-1, parseInt(d[0]),
 parseInt(t[0]-(now.getTimezoneOffset()/60)), parseInt(t[1]) 
)
};

arr.push(d);
arr.push(t);
arr.push(now);
arr.push(date2);
arr.push("now < date2");
arr.push(now < date2);

console.log(JSON.stringify(arr ,null,4))

 

 

 

 

 

 

 

Luxon  

 

Делаем время в красивм формате

  const dt = DateTime.fromSQL(date, { zone: 'UTC' }).toLocal()

// время когда был последний раз
import { DateTime } from 'luxon'
    timeAgo (date) {
      const units = [
        'year',
        'month',
        'week',
        'day',
        'hour',
        'minute',
        'second',
      ]
      let dateTime = DateTime.fromSQL(date, { zone: 'UTC' }).toLocal()
      const diff = dateTime.diffNow().shiftTo(...units)
      const unit = units.find((unit) => diff.get(unit) !== 0) || 'second'

      const relativeFormatter = new Intl.RelativeTimeFormat('en', {
        numeric: 'auto',
      })
      return relativeFormatter.format(Math.trunc(diff.as(unit)), unit)
    },

или так что равносильно

import { DateTime } from 'luxon'

export default function (date) {
  return DateTime
      .fromSQL(date, { zone: 'UTC' })
      .toLocal()
      .toRelative()
}

 

 

 

 

 

 Luxon date diff

      const start = DateTime.fromISO(this.start_date)
      const end = DateTime.fromISO(this.end_date)
      const diff = Math.floor(end.diff(start).as('days')) + 1
      const realStart = start.minus({ days: diff })
      const format = this.$route.params.lang === 'en' ? 'LL/dd/yyyy' : 'dd.LL.yyyy'
      console.log(`${realStart.toFormat(format)} - ${start.minus({ days: 1 }).toFormat(format)}`)