webroo / dummy-json

Generates random dummy JSON data in Node.js
MIT License
380 stars 61 forks source link

Implement current date/time #31

Closed LukaszGrela closed 4 years ago

LukaszGrela commented 5 years ago

I'd like to have a current date/time to be used, it could be as simple as implementing few keywords to use (now, nextDay,nextWeek,nextMonth...)

{{date 'now', 'nextDay'}}

or more complicated:)

{{date 'now', 'now+2d'}}

similar for the time

{{time 'now', 'nextHour'}}

or more complicated:)

{{time 'now', 'now+2h'}}
webroo commented 4 years ago

I like this idea but unfortunately the date parsing library I use (https://github.com/taylorhakes/fecha) does not support parsing natural language strings like now+2h or tomorrow. I will keep this idea in consideration for future features though.

If you just need "now" in your templates a simple helper like this will work:

const myHelpers = {
  dateNow: function() {
    const now = Date.now();
    return fecha.format(now, 'YYYY-MM-DD HH:mm'); // or whatever date formatting library you like
  }
};
const template = '{{dateNow}}';
const result = dummyjson.parse(template, { helpers: myHelpers });