Solved

Knockout js Date format problem in Bootstrap table

Posted on 2014-02-05
2
906 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
Why do we like using grid based layouts in website design? Let's look at the live examples of websites and compare them to grid based WordPress themes.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…

920 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

12 Experts available now in Live!

Get 1:1 Help Now