2

This is the value of my variable:

date.dateFrom = /Date(1328137200000)/

Here is how I use it:

$('#txtBoxDateFrom').val(data.dateFrom);

How to convert it to "normal" look? I consider normal look this: 01/01/2012

asked Mar 19, 2012 at 10:51
6

2 Answers 2

2
function FormatDate(d)
{
 var day = d.getDate();
 var month = d.getMonth() + 1;
 var year = d.getFullYear();
 return month + "/" + day + "/" + year;
}
var formatted = FormatDate(new Date(1328137200000));
// Sets it as 2/2/2012

If you want to pad it with zeros:

function FormatDate(d)
{
 var day = d.getDate();
 var month = d.getMonth() + 1;
 var year = d.getFullYear();
 return (month <= 9 ? '0'+month : month) + "/" + (day <= 9 ? '0'+day : day) + "/" + year;
}
var formatted = FormatDate(new Date(1328137200000));
// Sets it as 02/02/2012
answered Mar 19, 2012 at 11:25

2 Comments

but FormatDate (your function) always returns 3/19, even when the date is not that.
@Srcee I had a var d = new Date() in there as I was testing it. I updated it now and it should work fine.
0

Using Date.toString or some other related method (toDateString, toLocaleString, etc), depending on what you consider "normal" look.

answered Mar 19, 2012 at 10:53

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.