ABrowBoyGenius
ABrowBoyGenius

Reputation: 73

Naming File Dynamically By Date

I have been trying to get my express app to dynamically name the files that phantom.js produces monthly. My intention is to also have a database that timestamps and holds the name of each file so that it can be retrieved easily later.

My question is how to dynamically name the files based on the date they are generated.

My phantom.js module is as follows:

var page = require('webpage').create();

page.paperSize = {
  format:'Tabloid',
  orientation: 'landscape',
  margin: '1cm'
};
page.viewportSize = {
  width: 1980,
  height: 1080
};
page.open('<sitename>/', function() {
  page.render('./monthly.pdf');
  phantom.exit();
});

How do I dynamically name the monthly.pdf name on render. I have a cronjob that runs this script the first of every month. The idea is to be able to have a list of old monthly.pdf's that can be accessed through static files automatically based on the name and the database entry.

Any questions or clarification let me know.

Upvotes: 0

Views: 950

Answers (1)

brandonscript
brandonscript

Reputation: 72865

You can do this easily with Date():

var d = new Date();
var filename = './monthly-' + d.getDate() + '/' + d.getMonth() + '/' + d.getFullYear() + '.pdf';

Or even simpler using moment.js:

var filename = moment().format('[./monthly-]MMDDYYYY[.pdf]');

Upvotes: 3

Related Questions