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

3 Answers 3

Reset to default 2

Reference: 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