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

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
Sign up to request clarification or add additional context in comments.

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.