Convert string to date

OK, i am possibly being a bit lazy here but I am sure someone out there has a quick and easy way of doing this.

I have an API which returns dates as a string in the format like ‘18 Sep 2020’ which i want to convert into a date within a server action to insert into a database. What is the best way to do that?

I am using node

hello Brian

you can use client side js

function dateConvert() {
  var newDate = Date.parse("March 21, 2012");
  alert(newDate),
}

it will return “1332280800000”

or

function dateOtherConverter() {
var newDateConverter =  Date.parseDate("18 Sep 2020", "Y-m-d");
alert(newDateConverter );
}

try please

Need to do that within Server connect Serhat, it is inside an API repeat, i need to take the string and save it to database as a date. Think i need a custom formatter?

yes maybe you may need it because i guess not all functions have been transferred to nodejs yet or have not been tried. (I am not sure)

but at worst, you can use this code because nodejs is already javascript. or suitable formatter can be found in npm packages.

Created a custom formatter like this

const { padNumber, parseDate, formatDate } = require('../core/util');

module.exports = {

    dateconv: function dateOtherConverter(str) {

        if (str == null) return str;

        return parseDate(str, "Y-m-d");

    },

};

I get the error:
{“status”:“500”,“message”:“Date.parseDate is not a function”,“stack”:“TypeError: Date.parseDate is not a function\n at dateOtherConverter (/home/brian/nodejsapp/lib/formatters/custom.js:7:21)\n at /home/brian/nodejsapp/lib/core/parser.js:662:26\n at parser (/home/brian/nodejsapp/lib/core/parser.js:351:19)\n at Object.parseValue (/home/brian/nodejsapp/lib/core/parser.js:716:24)\n at App.parse (/home/brian/nodejsapp/lib/core/app.js:240:23)\n at App.setvalue (/home/brian/nodejsapp/lib/modules/core.js:103:26)\n at App._exec (/home/brian/nodejsapp/lib/core/app.js:219:57)\n at App._exec (/home/brian/nodejsapp/lib/core/app.js:201:28)\n at process._tickCallback (internal/process/next_tick.js:68:7)”}

Anyone any ideas?

Edit:
calling it like this (“datestring” it the input variable)

{{datestring.dateconv()}}

you can use npm modules @Hyperbytes
easy and useful

install

npm install date-and-time --save

example usage

I assumed that all the modules were already installed. At the top of the date.js formatter is see

const { padNumber, parseDate, formatDate } = require('../core/util');

which is why I added that line to the top of custom.js

What should the formatter do, what is the input and output?

Receive a text input like: “20 Sep 2020”
converted to a date to use in a database table date field

Please!

Discovered a bug in the util.js, here an update:

util.zip (1.3 KB)

Formatter should become something like:

const { formatDate } = require('../core/util');

module.exports = {

    dateconv: function(str) {
        if (str == null) return str;

        return formatDate(str, "yyyy-MM-dd");
    },

};
1 Like

You are not only a genius but also a very kind person - works perfectly thank you