node.js - changing time format in node js -
here fetching date stored in database in format(2013-08-17 07:50:21), getting below output, need date in format 17 august 2013 without using third party module. possible? if yes suggest solution.
var sql2 ="select `user_id`, `vote_date` `tb_post_votes` `post_id`=?"; connection.query(sql2,[postid],function(err, result) { var post={"user_id":result[0].user_id,"date":result[0].vote_date}); res.send(json.stringify(post)); }); output: {"user_id":11,"date":"2013-08-17t07:50:21.000z"}
javascript / nodejs not give format method date objects. can date, month , year provided native methods...
getyear() - gives year count 1990 113 2013 getfullyear() - gives full year 2013 getmonth() - gives month number 0 based index getdate() - gives date of month
also new date()
takes date in different formats , convert local date/time 1 of utc date format. if node server @ utc time can directly convert date date
constructor.
var date = new date(utc_date);
for specification can use getmonth()
in conjunction array
of months.
this should work you...
var months = ["january", "february", "march", "april", "may", "june", "july", "august","september", "october", "november", "december"]; var date = new date(result[0].vote_date); var datestr = date.getdate() +' ' + months[date.getmonth()] +' ' + date.getfullyear(); console.log(datestr); // gives 17 august 2013 var post={"user_id":result[0].user_id,"date":datestr});
you may find where can find documentation on formatting date in javascript? , working dates useful.
Comments
Post a Comment