0

I want to get the timestamp in data table . how to format my datetime in the jquery datatable function?

sql server datatime data, how to edit the columns so that i cant get the proper datetime in the data time. i have tried with type: 'date-dd-mmm-yyyy', but its not working

 2018-11-23 07:49:35.073
     data type - date time

javascript

$(document).ready(function () {
                            $('#myTable').DataTable({
                                "ajax": {
                                    "url": "/Temperature/loaddata",
                                    "type": "GET",
                                    "datatype": "json"
                                },
                                "columns": [
                                        { "data": "id", "autoWidth": true },
                                        { "data": "updatedDate", "autoWidth": true, type: 'date-dd-mmm-yyyy', targets: 0 },
                                        { "data": "deviceid", "autoWidth": true },
                                        { "data": "devicename", "autoWidth": true },
                                        { "data": "temp", "autoWidth": true },
                                        { "data": "faht", "autoWidth": true }
                                ]
                            });
                        });

c# code

public  ActionResult loaddata()
{ 
    using (smartpondEntities dc = new smartpondEntities())
    {
        var data = dc.Temperatures.OrderBy(a => a.id).ToList();
        return Json(new { data = data}, JsonRequestBehavior.AllowGet);
    }
}

json sample data

[{"id":1,"updatedDate":"2018-11-23T07:49:35.073","DeviceTime":null,"deviceid":1,"devicename":"aaaa","temp":28.50,"faht":87.90},{"id":2,"updatedDate":"2018-11-23T07:49:42.1","DeviceTime":null,"deviceid":1,"devicename":"aaaa","temp":28.50,"faht":87.90}]

output

/Date(1542939575073)/

exp op

23 Nov 2018 13:10:10 
1
  • Date is fine. It's just a matter of formatting to string. You need to format your date to expected when you displaying it. Commented Nov 23, 2018 at 13:22

3 Answers 3

1

i use moment.js for datetimes (docs). so in your render:

{
    title: "Date",// name 
    render: function (data, type, row) {//data
        return moment(row.updatedDate).format('DD/MM/YYYY hh:mm:ss');
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

You should use a render function to format/customize your data

For Ex:

"columns": [
{ "data": "engine" },
{ "data": "browser" },
{
  "data": "platform",
  "render": function ( data, type, row, meta ) {
               return ConvertToDate(data);
            }
}

Convert your date in ConvertToDate(data) function.

function ConvertToDate(data){
     return data;//Converted date
}

2 Comments

error saying - "ConvertToDate is not defined", do i need to make a another function
you need to add a function named ConvertToDate(date)
0

To render date format in Angular-Datatables, first you need to install npm moment

npm install moment --save

import * as moment from 'moment'; // import moment wherein you are using in component

{
  title: 'CreatedAt',
  data: 'CreatedAt',
  render: (data:any,type:any,full:any)=>{
    return moment(data).format('L'); 
// more related moment format you can check here - https://momentjs.com
   }
}

Comments

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.