0

I found a solution to transform a date like: Thu Sep 04 2014 00:00:00 GMT+0200 (Romance Daylight Time) to a format french 04/09/2014

How can i achieve this?

user1898765
  • 323
  • 1
  • 6
  • 18
  • possible duplicate of [Where can I find documentation on formatting a date in JavaScript](http://stackoverflow.com/questions/1056728/where-can-i-find-documentation-on-formatting-a-date-in-javascript) – uladzimir Nov 21 '14 at 10:26
  • http://stackoverflow.com/a/10119138/1815058 – uladzimir Nov 21 '14 at 10:27

2 Answers2

0

You can do that by using the Date object and its functions:

function zerofy(number){
    if(number < 10)
        number = "0" + number;

    return number;
}

var date = new Date("Thu Sep 04 2014 00:00:00 GMT+0200 (Romance Daylight Time)");

var day = zerofy(date.getDate());
var month = zerofy(date.getMonth());
var year = date.getFullYear();

var result = day + "/" + month + "/" + year

console.log(result);

Working Demo

I would recommend going over this document from MDN for these kind of problems.

Rahul Desai
  • 15,242
  • 19
  • 83
  • 138
  • Thanks for your solution, are there a way to determinate if the string receive is type of date? – user1898765 Nov 21 '14 at 11:10
  • @user1898765 Refer [this solution](http://stackoverflow.com/a/11249683/586051) for that problem. Please mark this answer as accepted if it has solved your problem. – Rahul Desai Nov 21 '14 at 11:36
0

Perhaps a basic one-liner would help?

function formatDate(date){
    return [('0'+date.getDate()).slice(-2),('0'+date.getMonth()).slice(-2),date.getFullYear()].join('/');
}
Sam Greenhalgh
  • 5,952
  • 21
  • 37