Reputation: 1965
I am using this dateTimePicker
for my angularjs app, and the issue I am having is that what the user enters as time gets translated to a different, UTC based time which differs many hours (screen shot below, the component and the text next to it are both using the same ng-model
).
Is there anyway to force this component to produce the output exactly as user enters? like in this case, I would like to get "2016-05-24 12:00 AM" out of it!
Upvotes: 1
Views: 97
Reputation: 2002
You can use this Code :
var d = new Date($scope.date);
var day = d.getDate();
var mon = d.getMonth()+1;
var year = d.getFullYear();
var hour = d.getHours();
var min = d.getMinutes();
if(day <10) day = "0"+day;
if(mon <10) mon = "0"+mon;
$scope.newDate = day+"-"+mon+"-"+year+" "+ hour + ":"+ min;
alert($scope.newDate);
To convert this 24 hour to 12 hour clock , follow these two question's answers .
Converting 24 hour time to 12 hour time w/ AM & PM using Javascript
Javascript: convert 24-hour time-of-day string to 12-hour time with AM/PM and no timezone
Upvotes: 1
Reputation: 1720
Use the date filter and leave the timezone empty on your date value, leaving the timezone empty will use browser default timezone:
{{date | date: 'yyyy-MM-dd hh:mm:ss Z' : ''}}
reference: https://docs.angularjs.org/api/ng/filter/date
Upvotes: 1