Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I display todays date in Node.js Jade?

Tags:

date

node.js

pug

I am new to Node.js and Jade and I have tried to use #{Date.now()} and it's giving me numbers. How do I display the date in mm/dd/yy format?

like image 218
Vartan Arabyan Avatar asked Sep 14 '12 06:09

Vartan Arabyan


People also ask

How do I get the current date in node js?

Since Node. js is based on JavaScript, you can use the Date object methods getDate() , getMonth() , and getFullYear() to get the current day, month, and year in a Node.

What format is date now ()?

Custom Date Formatter Function It could be in yy/dd/mm or yyyy-dd-mm format, or something similar.

How do you check if the date is today's date in JavaScript?

To check if a date is today's date:Use the Date() constructor to get today's date. Use the toDateString() method to compare the two dates. If the method returns 2 equal strings, the date is today's date.

How do I pass a date in node js?

Either use ISO 8601 format as Phil suggested, or simply pass the date as milliseconds (since 1970). For example, new Date(1447378736842) is the same as new Date("2015-11-13T01:38:56.842Z") . To get the current date in ISO 8601 format, you might do something like this: var d = new Date(); var n = d.


2 Answers

You can use moment.js

First, add moment to your express application locals

express = require('express'); ... app = express(); app.locals.moment = require('moment'); 

Then you can use moment within a jade template like this:

p #{moment(Date.now()).format('MM/DD/YYYY')} 

Moment takes Date.now() by default, so yo can also write:

p #{moment().format('MM/DD/YYYY')} 

Sources:

  • Making use of utility libraries in server-side Jade templates
  • Moment.js
like image 144
Yves M. Avatar answered Sep 20 '22 04:09

Yves M.


This is old, but I do the following:

return (new Date()).toLocaleDateString() 

Returns a date 'mm/dd/yyyy' format, in no additional libs required.

like image 38
Gadi Avatar answered Sep 19 '22 04:09

Gadi