Click here to Skip to main content
15,895,841 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am getting date in 2014-02-20T22:04:58.797 format but i want date in
02/20/2014 10:04 pm.My code is
C#
function dispose() {
           if (currentSelectedRows != null) {
               var date = currentSelectedRows.data.DispDate;
               str = "<table cellspacing="0" cellpadding="0" style="width:75%">";
               str += + "<tr style="height:15px"><th align="left" style="font-weight:bold; color:black; font-size:12px; height:15px;">Ir user wrote :</th><td style="text-align:left; font-size:12px;">" + currentSelectedRows.data.DispDate + "</td></tr>"
               +"</table>";
           }

       }

Here i am getting dispdate = 2014-02-20T22:04:58.797 and i want date in 02/20/2014 10:04 pm format.
Please help me.
Posted
Updated 24-Nov-14 1:32am
v4

There are multiple libraries (Date.js, moment.js and others) that take care of this...
Also, it is easy to find googling, please do due dilligence next time before asking.



If you want your own function write it. Something like this:
HTML
<script type="text/javascript">
    var d = new Date();
    var curr_day = d.getDate();
    var curr_month = d.getMonth() + 1; //Months are zero based
    var curr_year = d.getFullYear();
    document.write(curr_day+ "/" + curr_month + "/" + curr_year);
</script>


There are getHour and others to get however precise value you need.


If this helps please take time to accept the solution.
 
Share this answer
 
The best one i used so far is Moment.js[^].

- Download script.

- Add reference of it to the page and you are ready to go.

Below code should work for you.
C#
var Available_Date = "2014-02-20T22:04:58.797"; 

var Required_Date = moment(Available_Date).format("MM/DD/YYYY hh:mm a");

Always remember, Date doesn't have any format,never. You can convert date into format you require,but that result will be string,not Date.

In our case, Required_Date is string representation of Available_Date

Regards..
 
Share this answer
 
v2
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900