this.convertJSON_DateTimeTo_datetimeObj = function (jsonDate) {
var date_dd_MM_yyyy = $filter('date')(jsonDate.slice(6, -2), 'medium');
//var dt = new Date(date_dd_MM_yyyy).toLocaleString("en-US", { timeZone: "Asia/Kolkata" });
//var dt = new Date(date_dd_MM_yyyy.split("/").reverse().join("/"));
return date_dd_MM_yyyy;
},
Input Parameter jsonDate = "/Date(1629810881857)/"
In the above code, I want to convert JSON DateTime into DateTime Object where the output timezone is "Asia/Kolkata". code in the comment is that which is already tried by me. It's very helpful for me if any body help me Thanks
CodePudding user response:
There are a few problems with this. First, the server isn't sending any timezone information in the /Date(1629810881857)/
string. This means you can't safely convert it to Asia/Kolkata timezone because you don't know what you're converting it from. You need to get the server to send that information in its response.
Once you solve that, you need to parse the date string into a JavaScript Date
properly. Here's the best way. The $filter
service shouldn't be needed here.
Lastly, it's best practice to let JavaScript dates convert to the end user's locale as defined in their browser/OS rather than forcing it to display in a particular timezone such as Asia/Kolkata. However, if you want to always display a particular timezone no matter where the user is located, here is a great answer explaining how that works.