Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass long number through javascript?

I want to convert numberLong date from the mongo db database into dd/mm/yyyy format in javascript.

When I put direct hardcoded value like the following code, it gives me correct result:

function getDateIfDate(d) {
    var m = d.match(/\/Date\((\d+)\)\//);
    return m ? (new Date(+m[1])).toLocaleDateString('en-US', {month: '2-digit', day: '2-digit', year: 'numeric'}) : d;
}

console.log(getDateIfDate("/Date(1460008501597)/"));

Here is my code :

for(var i=0;i<keys;i++)
                {
                var tr="<tr>";
                tr+= "<td><input type='checkbox' name='record'></td>"
                tr+="<td>"+positionList[i]["fromDate"]+"</td>";
                var j = (positionList[i]["fromDate"]);
                console.log("value of j is =========="+j);
                console.log(getDateIfDate("/Date(j)/")); // actual conversion should happen here
}

What changes I should make in my code to get the date in required format?

like image 653
Prachi Patil Avatar asked Jan 26 '26 10:01

Prachi Patil


2 Answers

tr+="<td>"+positionList[i][new Date("fromDate").toLocaleString()]+"</td>";

try to replace old line by this new one

like image 152
BritSys Avatar answered Jan 28 '26 22:01

BritSys


You can use momentjs for your case. It's really simple to use, if you want to format your date to DD/MM/YYYY, just add the row below:

var formattedDate = moment(new Date()).format("DD/MM/YYYY");
like image 35
Commercial Suicide Avatar answered Jan 29 '26 00:01

Commercial Suicide



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!