admin管理员组文章数量:1426338
This date issue is giving me a hard time. i am getting the below value from sql table.
var CurrentDate = 2017-04-25T00:00:00
I need to convert this to 04/25/2017 at the UI. I tried using various date methods but didnt get wat i want. Can someone pls shed some light. this is wat i tried.
var date_new = new Date(CurrentDate);
date_new= date_new.toLocaleDateString();
This date issue is giving me a hard time. i am getting the below value from sql table.
var CurrentDate = 2017-04-25T00:00:00
I need to convert this to 04/25/2017 at the UI. I tried using various date methods but didnt get wat i want. Can someone pls shed some light. this is wat i tried.
var date_new = new Date(CurrentDate);
date_new= date_new.toLocaleDateString();
Share
Improve this question
edited Apr 25, 2024 at 14:30
mplungjan
179k28 gold badges182 silver badges240 bronze badges
asked Apr 26, 2017 at 23:44
RihanaRihana
4432 gold badges7 silver badges14 bronze badges
2
- When I run your code (putting ' ' around the CurrentDate string), and print date_new to the console, I get what you want ('04/25/2017'). Are you getting an error, or a string that's not what you want? – cjg Commented Apr 26, 2017 at 23:51
- @Rihana, you may try executing the snippet from my answer. – Devendra Lattu Commented Apr 27, 2017 at 0:38
3 Answers
Reset to default 2Reference: MDN
var CurrentDate = "2017-04-25T00:00:00";
var date_new = new Date(CurrentDate);
document.write(new Intl.DateTimeFormat('en-US').format(date_new));
You could use momentjs to parse your dates and times, it's very easy and powerful.
Install moment and then require it in your js.
const moment = require('moment')
const currentDate = '2017-04-25T00:00:00'
const parsedCurrentDate = moment(currentDate).format('l')
And you will get: '4/25/2017'
stored in your parsedCurrentDate
variable.
Just show that in your ui.
There are other formatting options, just take a look at the moment's website.
You are almost there, just surround your date in quotes.
var CurrentDate = '2017-04-25T00:00:00';
本文标签: Convert Date to mmddyyyy format in javascriptStack Overflow
版权声明:本文标题:Convert Date to mmddyyyy format in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745460092a2659284.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论