Phoenix
Phoenix

Reputation: 1528

How to convert data format "YYYY-mm-dd hh:mm:ss" to "dd-mm-YYYY hh:mm:ss" using javascript?

I have datetime column (format in YYYY-mm-dd hh:mm:ss) in my database.

I need to convert it into dd-mm-YYYY hh:mm:ss format.

How can i do? Help me.

Upvotes: 0

Views: 1386

Answers (4)

Dropout
Dropout

Reputation: 13866

I think there isn't a way in which you can convert time formats in JavaScript by default, however you can achieve the requested result with the following function:

var input = "2000-12-24 12:30:59";

var date = new Date(input);

var output = "";
if(date.getDate()<10){
    output += "0";    
}
output += date.getDate() + "-";
if(date.getMonth()<10){
    output += "0";    
}
output += date.getMonth() + "-";
output += date.getYear() + " ";
if(date.getHours()<10){
    output += "0";    
}
output += date.getHours() + ":"
if(date.getMinutes()<10){
    output += "0";    
}
output += date.getMinutes() + ":";
if(date.getSeconds()<10){
    output += "0";    
}
output += date.getSeconds();

alert(output);

Please check out http://www.w3schools.com/jsref/jsref_obj_date.asp

Upvotes: 0

Girish
Girish

Reputation: 12127

Not sure, how to get db value in javascript, but if you have data in javascript variable then you can do this way

var date_arr = "2014-11-26 05:04:13".split(" ");
var date_aar2 = date_arr[0].split("-");
var new_date = date_aar2[2] + "-" + date_aar2[1] + "-" + date_aar2[0] + " " + date_arr[1];
console.log(new_date);

Upvotes: 2

Saajan
Saajan

Reputation: 680

function change(time) {
    var r = time.match(/^\s*([0-9]+)\s*-\s*([0-9]+)\s*-\s*([0-9]+)(.*)$/);
    return r[3]+"-"+r[2]+"-"+r[1]+r[4];
}
var valuee = change("2014-02-14 03:00:00");

alert(valuee);

Upvotes: 0

Phoenix
Phoenix

Reputation: 1528

Split the datetime column and concat using comma and colon like below.

var date_time = "DATETIME".split(/-|\s|:/);  // Here DATETIME is "yyyy-mm-dd hh:mm:ss"
var date1 = new Date(arr[2]+"-"+arr[1]+"-"+arr[0]+" "+arr[3]+":"+arr[4]+":"+arr[5]);
alert(date1)

Result : 26-11-2014 06:12:56

Upvotes: 0

Related Questions