Click here to Skip to main content
15,029,039 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to format the string, currency, datetime and Number in javascript?
Posted
Comments
Ankur\m/ 5-Dec-10 23:53pm
   
Have you Googled it?
Kashsolai 6-Dec-10 4:34am
   
I have googled it. There are some methods like, Reqular expression methods, JQuery plugins But i need an method that use the pattern to format.

In C# I am using like Format("{OrderDate:MM/dd/yyyy}"), OrderDate is the field name. result will neglect the time in OrderDate field and just give the result 10/26/2010 i.e. in MM/dd/yyyy order.
Sandeep Mewara 6-Dec-10 14:17pm
   
What do you mean by format here?
Kashsolai 6-Dec-10 23:37pm
   
Format("{OrderDate:MM/dd/yyyy}") input comes from OrderID field 26/10/2010 12:00:00 output is 10/26/2010.

Thank you for your question. You can follow the bellow code.

C#
function CommaFormatted(amount)
{
    var delimiter = ","; // replace comma if desired
    var a = amount.split('.',2)
    var d = a[1];
    var i = parseInt(a[0]);
    if(isNaN(i)) { return ''; }
    var minus = '';
    if(i < 0) { minus = '-'; }
    i = Math.abs(i);
    var n = new String(i);
    var a = [];
    while(n.length > 3)
    {
        var nn = n.substr(n.length-3);
        a.unshift(nn);
        n = n.substr(0,n.length-3);
    }
    if(n.length > 0) { a.unshift(n); }
    n = a.join(delimiter);
    if(d.length < 1) { amount = n; }
    else { amount = n + '.' + d; }
    amount = minus + amount;
    return amount;
}


Date Format

C#
var dt = new Date();
var dtstring = dt.getFullYear()
    + '-' + pad2(dt.getMonth()+1)
    + '-' + pad2(dt.getDate())
    + ' ' + pad2(dt.getHours())
    + ':' + pad2(dt.getMinutes())
    + ':' + pad2(dt.getSeconds());


Ref:http://www.web-source.net/web_development/currency_formatting.htm[^]

Thanks,
Mamun
   
v3
Comments
fjdiewornncalwe 12-Dec-10 10:05am
   
My vote of 1. This is clearly not your code. Do not plagarize someone's code without giving due credit. Original source was posted by William Bontrager (http://www.web-source.net/web_development/currency_formatting.htm)
Abdul Quader Mamun 12-Dec-10 23:31pm
   
We are too lazy to search. That's why ask question.
Abdul Quader Mamun 12-Dec-10 23:48pm
   
Have you ever MSDN code sample?
fjdiewornncalwe 13-Dec-10 14:24pm
   
I have never posted someone else's code without proper credit. That's just dishonest and wrong.
Use Microsoft Ajax toolkit to achieve your goal.
in javascript it is called MASK.
There is a control in Ajax toolkit in which you can use Extended controls in HtmlControls like input html control.
   

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