Tuesday, May 14, 2024
 Popular · Latest · Hot · Upcoming
196
rated 0 times [  200] [ 4]  / answers: 1 / hits: 15587  / 9 Years ago, tue, march 17, 2015, 12:00:00

I have a mongodb set up and I'm storing date and time alongside other data. The problem is that when I receive the data back the date and time is in a strange format and I'm not sure how I can handle this with JavaScript or jQuery.



my schema:



var carSchema = mongoose.Schema ({
carType: String,
notes: String,
created: {type: Date, default: Date.now}
});


this is what I'm getting in the JavaScript object:



created: 2015-03-15T14:01:16.447Z


How can I convert this to Time and date?



Can anyone help please?


More From » jquery

 Answers
1

Here i am giving some snippet of codes which will assist to getting your requirement.



var created_date = new Date(carSchema.created);

var months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
var year = created_date.getFullYear();
var month = months[created_date.getMonth()];
var date = created_date.getDate();
var hour = created_date.getHours();
var min = created_date.getMinutes();
var sec = created_date.getSeconds();
var time = date + ',' + month + ' ' + year + ' ' + hour + ':' + min + ':' + sec ; // final date with time, you can use this according your requirement



Or

var timestamp = created_date.getTime(); // get time stamp, now you can convert date and time from it using simple JavaScript function


You can use time() npm to reset timezone for this



var time = require('time');

created_date.setTimezone(Australia/Sydney);


Thanks


[#67399] Monday, March 16, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
hallie

Total Points: 503
Total Questions: 114
Total Answers: 103

Location: Iraq
Member since Fri, Jun 5, 2020
4 Years ago
;