1

So I have a notice section on my site which displays what date the notice was created. When showing the createdAt date it displays like this -

Thu Mar 03 2022 15:11:22 GMT+0000 (Greenwich Mean Time)

Instead I would like it display as

03/03/2022 - 15:11

Thats DD/MM/YEAR.

Here is my Schema and my EJS that I am displaying it with. Also how it is stored in the database.

Schema

const noticeSchema = new mongoose.Schema({
noticeTitle: {
    type: String,
    required: true
},
noticeText: {
    type: String,
    required: true
},
author: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'User'
}
}, {timestamps: true 
}); 

EJS

<% notices.map(notice => notices.createdAt ) %>
<%= notice.createdAt %>

Database Entry

_id: new ObjectId("6220ead4f238fc30822e5e6d"),
noticeTitle: 'test3',
noticeText: '<p>test3</p>',
author: {
  _id: new ObjectId("621e06a6c29dc2273e412537"),
  firstName: 'test',
  lastName: 'test',
  jobRole: 'Other',
  email: 'test',
  username: 'DeneHCAdmin',
  __v: 0
},
createdAt: 2022-03-03T16:20:36.680Z,
updatedAt: 2022-03-03T16:20:36.680Z,
__v: 0
}

Can anybody help?

sgledhill
  • 53
  • 6
  • `let date = new Date('Thu Mar 03 2022 15:11:22 GMT+0000 (Greenwich Mean Time)') console.log(date.toLocaleDateString('en-GB') + ' - ' + date.toISOString().substring(11,16))` – cmgchess Mar 03 '22 at 17:10

2 Answers2

1

It's as easy as splitting the Date into several constants and connecting them together

const creationDate = new Date(),
  dateYear = creationDate.getFullYear(),
  dateMonth = creationDate.getMonth(),
  dateDay = creationDate.getDay(),
  dateHour = creationDate.getHours(),
  dateMinute = creationDate.getMinutes();

const dateFullTime = `${dateMonth}/${dateDay}/${dateYear} - ${dateHour}:${dateMinute}`

console.log(dateFullTime)
// expected output dd/mm/yyyy - hh:hh (if you'd want a zero before the month and day when it's only one character, you would have to make an easy if statement.)
wateroverdose
  • 79
  • 1
  • 9
  • So I've tried this and it makes a new date in my console in the correct format, but how would i apply this to my timestamp in the Schema? sorry if this sounds stupid, this is the first ever website I've built with code.... figuring things out as i go. – sgledhill Mar 04 '22 at 13:12
  • @sgledhill unfortunately i can't really help since i've never played with databases. since you're trying to edit the value, i've searched a bit and found this stackoverflow thread https://stackoverflow.com/a/57062391/17747971. if I understand it correctly, you could just change the item["name"] = "new name" with item['createdAt'] = dateFullTime. I hope this helps somehow – wateroverdose Mar 04 '22 at 14:13
0
const t = new Date();
const date = t.getDate();
const month = t.getMonth();
const year = t.getFullYear();
const hours = t.getHours();
const minutes = t.getMinutes();
const time = `${date}/${month}/${year} - ${hours}:${minutes}`;

It is just simple date manipulation in javascript, doesn't have anything to do with mongoose itself

hafizur046
  • 306
  • 3
  • 8