admin管理员组文章数量:1292284
i am using nodejs to connect to mySql and connection works. while using query such as
SELECT * from someTable
i get the required results. But the problem is that i am having a column which stores date is having the given format. before sending the data i need this format to be converted and vice versa
file_date : Wed Jan 01 2014 05:34:53 GMT+0530 (IST)
This is format i am getting
i am unable to send params with Date in this format from client side so i need to convert this data to "DD-MM-YYYY" format.i need this way so that i can pass params in "DD-MM-YYYY" and fetch data date wise
i am using nodejs to connect to mySql and connection works. while using query such as
SELECT * from someTable
i get the required results. But the problem is that i am having a column which stores date is having the given format. before sending the data i need this format to be converted and vice versa
file_date : Wed Jan 01 2014 05:34:53 GMT+0530 (IST)
This is format i am getting
i am unable to send params with Date in this format from client side so i need to convert this data to "DD-MM-YYYY" format.i need this way so that i can pass params in "DD-MM-YYYY" and fetch data date wise
Share asked Jul 28, 2015 at 11:14 SiddharthSiddharth 5452 gold badges7 silver badges25 bronze badges 1- when you send and receive data use moment("Wed Jan 01 2014 05:34:53 GMT+0530").format("DD-MM-YYYY"); – rjdmello Commented Jul 28, 2015 at 12:04
1 Answer
Reset to default 7Simple Approach
If you know your date format is supported in Moment, then the simplest approach is to construct a moment object and format it.
var file_date = 'Wed Jan 01 2014 05:34:53 GMT+0530 (IST)';
var formatted = moment(file_date).format('DD-MM-YYYY');
Deprecation Warning:
However, at some point support for non ISO date formats was deprecated. It will in some cases still work but you should be warned (per the deprecation warning message):
Deprecation warning: value provided is not in a recognized ISO format. moment construction falls back to js Date(), which is not reliable across all browsers and versions. Non ISO date formats are discouraged and will be removed in an uping major release.
Workaround
Instead, if you know the format of an input string, you can use that to parse a moment.
var s = 'Wed Jan 01 2014 05:34:53 GMT+0530 (IST)';
var m = moment(s, 'ddd MMM DD YYYY hh:mm:ss [GMT]ZZ').format('MM-DD-YYYY');
console.log(m);
<script src="https://momentjs./downloads/moment.min.js"></script>
When specifying a format manually like this, it helps to have the format documentation handy: https://momentjs./docs/#/displaying/format/
本文标签: javascriptMomentjs Sql Date Time Conversion with NodejsStack Overflow
版权声明:本文标题:javascript - Moment.js Sql Date Time Conversion with Nodejs - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741552860a2384979.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论