Click here to Skip to main content
15,849,406 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I want to generate data from datatable but the date column gives me
2022-09-08T18:05:09.0541780+05:30


i want in
YYYY-MM-DD HH:mm:ss
format

need help TIA

What I have tried:

{
                       "sDefaultContent": "", "data": "Date", "width": 90, "title": "Date", render: function (data, type, full, meta) {
                           var dateSplit = data.split('(');
                           var dateVal = parseFloat(dateSplit[1].split(")")[0]);
                           var s = new Date(dateVal).toISOString();
                           return moment(s).format("YYYY-MM-DD'T'HH:mm:ss:ssssss");
                       }
                   }


but it doesn't load the datatable
Posted
Updated 12-Sep-22 21:49pm

1 solution

You are assuming that your data contains a Date value enclosed in parentheses:
(2022-09-08T18:05:09.0541780+05:30)
which is not what you say teh table contains.

Either the table contains the date as a string - which is a very bad idea and menas your DB needs to be changed, along with all the software that works with it - or it contains a DATE / DATETIME field in which case it isn't a string at all and won't contain any parentheses.

So start with your DB: look at the table definition and see exactly what and how you are storing your info. If you aren't storing numeric values in numeric fields such as INT, FLOAT, or DECIMAL; and date related into in DATE, DATETIME, or DATETIME2 columns then you need to fix that first.

When you have date related data, you can format your display as you want without the need to faff with strings.
 
Share this answer
 
Comments
Manojkumar-dll 13-Sep-22 2:58am    
Yes you are abosultely right the problem i faced because the date field is marked as string after i change it to DateTime and changed
return moment(s).format("YYYY-MM-DD HH:mm:ss");

it works fine

Thank you so much!
OriginalGriff 13-Sep-22 3:52am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900