admin管理员组文章数量:1336392
I am using a form ,from which the user can select the datetime from datetime picker in dd-mm-yyyy hh:mm:ss
format. Now I want to convert the format to yyyy-mm-dd hh:mm:ss
format to store in mysql table.
I tried with moment js like this
console.log(moment(status.date).format('MM/DD/YYYY'));
where status.date I will post from a form where the user selects datetime from datetimepicker.
Please help
I am using a form ,from which the user can select the datetime from datetime picker in dd-mm-yyyy hh:mm:ss
format. Now I want to convert the format to yyyy-mm-dd hh:mm:ss
format to store in mysql table.
I tried with moment js like this
console.log(moment(status.date).format('MM/DD/YYYY'));
where status.date I will post from a form where the user selects datetime from datetimepicker.
Please help
Share Improve this question asked Aug 26, 2015 at 3:45 Niranjan N RajuNiranjan N Raju 12k4 gold badges23 silver badges41 bronze badges 5-
Why convert to string? Just give
node-mysql
aDate
object in the prepared statement. – Amadan Commented Aug 26, 2015 at 3:50 - Check out the formats momentjs./docs/#/displaying/format – Swaraj Giri Commented Aug 26, 2015 at 3:51
- @amadan, the backend is already built in php codeigniter, now we are waiting api's in nodejs. So we want to convert to standard mysql format. – Niranjan N Raju Commented Aug 26, 2015 at 3:55
- @SwarajGiri there we can change the format, but im giving the date from form, so I should be able to change the format of the post value. – Niranjan N Raju Commented Aug 26, 2015 at 3:56
-
If you get in a string and want to output a string, you can remove any external library requirement by just rearranging the parts:
s.replace(/(^\d\d)(-\d\d-)(\d{4})(.+$)/,'$3$2$1$4')
. – RobG Commented Aug 26, 2015 at 3:58
3 Answers
Reset to default 2You can do like this instead of having some modules.
var fd = status.date;
var fromDate = fd.split(" ");
console.log(formatDate(fromDate[0],fromDate[1] + " " + fromDate[2]));//
and add these functions there.
function formatDate(date, time2) {
var from = date.split("-");
var f = from[2] + "-" + from[1] + "-" + from[0];
var time1 = time(time2);
return f + " " + time1;
}
function time(time) {
var hours = Number(time.match(/^(\d+)/)[1]);
var minutes = Number(time.match(/:(\d+)/)[1]);
var AMPM = time.match(/\s(.*)$/)[1];
if ((AMPM == "PM" || AMPM == "pm") && hours < 12)
hours = hours + 12;
if ((AMPM == "AM" || AMPM == "am") && hours == 12)
hours = hours - 12;
var sHours = hours.toString();
if (hours < 10)
sHours = "0" + sHours;
if (minutes < 10)
sMinutes = "0" + sMinutes;
return (sHours + ":" + sMinutes);
}
Convert it to ISOString first and then eliminate unwanted things by replace
method.
var date = new Date();
date.toISOString().replace(/T/, " ").replace(/\..+/,'')
source: chbrown's answer
Building on top of Prateek Jain's ISOString method, we can use toLocaleString
to obtain the local time in ISO format.
One needs to shop around the base format provided by toLocaleString
according to different regions. The format closest to ISO is en-CA
(refer to this answer for all the region-based format). We then use the option {hour12: false}
to convert to 24-hour notation.
const d = new Date();
d.toLocaleString('en-CA', {hour12: false}).replace(/, /, ' ');
本文标签: javascriptDate format conversion in node jsStack Overflow
版权声明:本文标题:javascript - Date format conversion in node js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742257409a2441881.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论