最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Date format conversion in node js - Stack Overflow

programmeradmin5浏览0评论

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 a Date 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
Add a ment  | 

3 Answers 3

Reset to default 2

You 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(/, /, ' ');
发布评论

评论列表(0)

  1. 暂无评论