Solved

Knockout js Date format problem in Bootstrap table

Posted on 2014-02-05
2
894 Views
Last Modified: 2014-02-08
I am new to knockout js:

I am trying to display a Date in a Bootstrap table using knockout js data binding

The Json data coming across from the server includes a CreatedDate

[{"Id":"000eb141-cb55-4582-a4f9-0f428e77ae1f","CreatedDate":"\/Date(1391628264240)\/","Reference":"8888","Notes":"bddddd","TotalAmount":96.00,"Status":"Draft","CostCentre":"Science","NextStatus":"Sent For Approval","NextActionText":"Send For Approval","CanDelete":true,"CanAuthorise":false,"CanExecuteNextAction":true

When I bind the date in my bootstrap table
 <td data-bind="text: CreatedDate"></td>

I get:  /Date(1391628264240)/  displayed in the CreatedDate column.

What do I have to do to get the date to display in dd/MM/yyyy format?
0
Comment
Question by:ccravenbartle
  • 2
2 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 39838859
Format the date on the server side :
I know you do a query from the database and just apply json_encode to the full result but you need to loop over each resulted row and do something similar to :

	$CreatedDate = '/Date(1391628264240)/';
	preg_match("/[0-9]+/", $CreatedDate, $matches);
	$CreatedDate = date("d-m-Y", $matches[0]/1000);

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
ID: 39839560
Test page if you're able to loop over data on the client side : http://jsfiddle.net/mX28D/

var arr = [
  {
    "CreatedDate": "\/Date(1391628264240)\/",
    "Reference": "8888",
    "Notes": "bddddd",
    "TotalAmount": 96.00,
    "Status": "Draft",
    "CostCentre": "Science",
    "NextStatus": "Sent For Approval",
    "NextActionText": "Send For Approval",
    "CanDelete": true,
    "CanAuthorise": false,
    "CanExecuteNextAction": true
  }
];

var ViewModel = function(first, last) {
    this.CreatedDate = ko.observable("06/02/2014");
};

var m = new ViewModel();
ko.applyBindings(m);

var date = arr[0].CreatedDate;
date = date.replace(/\D/g,""); // remove parentheses
date = date * 1; // It's now a number
date = new Date(date);
var d2 = function(n) { return n>9?n:"0"+n; };
date = d2(date.getDate()) + "/" + d2(date.getMonth()+1) + "/" + date.getFullYear();
m.CreatedDate(date); // 05/02/2014

Open in new window

0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

"In order to have an organized way for empathy mapping, we rely on a psychological model and trying to model it in a simple way, so we will split the board to three section for each persona and a scenario and try to see what those personas would Do,…
Any business that wants to seriously grow needs to keep the needs and desires of an international audience of their websites in mind. Making a website friendly to international users isn’t prohibitively expensive and can provide an incredible return…
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now