0

I use datatables.net plugin for jquery to build a grid.

I need to output the column date. From the database I get this:

{ts '2013-05-04 03:21:12'}

I would like to output something as 04-05-2013

2 Answers 2

1

Split the string up and rearrange it.

var ts = "{ts '2013-05-04 03:21:12'}";
ts = ts.match(/'([^']+)'/)[1];  // or ts.split("'")[1]
var dt = ts.split(" ");
var date = dt[0];
var dateSplit = date.split("-");
var finalDate = dateSplit[2] + "-" + dateSplit[1] + "-" + dateSplit[0];

This is assuming the timestamp will always be in the format you provided (won't have other numbers/characters or be in different order).

DEMO: http://jsfiddle.net/GQthr/1/

Sign up to request clarification or add additional context in comments.

6 Comments

If you split on ' instead, you can deal with the original {ts ...}.
@Mogsdad Instead of splitting on ', I'd rather use a regex. Anyways, I wasn't sure what the OP actually had access to - whether it was the full string or just the date/time. But that's a good point, so I updated my answer to hopefully accommodate that.
That {ts ...} is a common way that dates are expressed in some SQL installations. So it's a fair guess that the whole string is available.
@Mogsdad Didn't know that, so thanks for the info :) Does the update to my answer look/work right then?
Yeah... although I dislike resorting to regex. Personal pref.
|
0

Here's another method, that takes advantage of Date.parse() and it's support for ISO 8601 formatted dates (which is what you have). I borrowed from @Ian's answer for the variable names.

var ts ="{ts '2013-05-04 03:21:12'}";

var dt = ts.split("'");
var date = new Date(Date.parse(dt[1].replace(' ','T')));
var finalDate = date.getDate() + "-" + (date.getMonth()+1) + "-" + date.getFullYear();

alert(finalDate);

1 Comment

Might as well fork my fiddle and use your code to show this works too!

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.