0

I have array of dates in sql format. So dates[0]="2001-01-01", I need to format it in format: "1-Jan-12". As I found it here it should work like this.

tmpDate=Date.parse(dates[0]);
var date=tmpDate.format("dd-mm-yy");

it says: Uncaught TypeError: Object 978307200000 has no method 'format' So type of my object is string however it should be date.

console.log($.type(tmpDate));

Also says "number". Why?

4
  • 7
    The link you posted is suggesting to use a Date Format library. Have you included this library in your project? This functionality is not built into native JS. Commented Mar 16, 2014 at 5:20
  • i think same question asking here yesterday ? Commented Mar 16, 2014 at 5:31
  • Yep, the same question. Commented Mar 16, 2014 at 7:03
  • Yep, my mistake. But Ben found better sollution Commented Mar 16, 2014 at 7:09

2 Answers 2

1

Sometimes it is simpler to use plain javascript without plugins or 3rd party libraries. If that is what you are going for, there is some great documentation of the Date object and methods at http://www.w3schools.com/jsref/jsref_obj_date.asp.

Here is the simplest way to do this in javascript from http://jsfiddle.net/bbankes/pVU24/

var monthAbbreviations = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];

var dates = ['2001-01-01'];
var tmpDate = new Date(dates[0]);

var day = tmpDate.getUTCDate();
var mo = monthAbbreviations[tmpDate.getUTCMonth()];
var yr = String(tmpDate.getUTCFullYear()).substring(2,4);

var formattedDate = day + '-' + mo + '-' + yr;
Sign up to request clarification or add additional context in comments.

Comments

0

According to W3 Schools, Date.parse() returns the number of milliseconds since Jan 1, 1970. Thus you are getting back a number.

I think you are looking for this: tmpDate = new Date (dates [0]);

Comments

Your Answer

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.