3

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

Niranjan N Raju
  • 12,047
  • 4
  • 22
  • 41
  • Why convert to string? Just give `node-mysql` a `Date` object in the prepared statement. – Amadan Aug 26 '15 at 03:50
  • Check out the formats http://momentjs.com/docs/#/displaying/format/ – Swaraj Giri Aug 26 '15 at 03: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 Aug 26 '15 at 03: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 Aug 26 '15 at 03: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 Aug 26 '15 at 03:58

3 Answers3

2

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

Community
  • 1
  • 1
Prateek Jain
  • 1,504
  • 4
  • 17
  • 27
  • Not sure that will suit. It returns the time as UTC. The OP seems to want local, though not stated explicitly. I seem to recall that not all implementations return the decimal seconds, but not certain about that (and may not be an issue with node.js). – RobG Aug 26 '15 at 04:02
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);

}

0

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(/, /, ' ');
Fanchen Bao
  • 3,310
  • 1
  • 21
  • 34