1
var date = "2015-12-16";
//convert to "16 Dec 2015"
var time = "17:04:43";
//Convert to "5:04pm"

How to convert date & time format using AngularJS or Javascript?

gariepy
  • 3,576
  • 6
  • 21
  • 34
Bharat Chauhan
  • 3,204
  • 5
  • 39
  • 52

5 Answers5

2

Here you go:

var date = "2015-12-16";
var dateInstance = new Date(date);

Now inject the $filter in your controller/server/diretive you want and write like this:

$filter("date")(dateInstance, "MMM dd yyyy") == "16 Dec 2015"

If you want to do this in your HTML, then bind this to $scope

$scope.dateInstance = dateInstance;

and in HTML:

{{dateInstance | date: 'MMM dd yyyy'}}
Shashank Agrawal
  • 25,161
  • 11
  • 89
  • 121
1

You can use filters in angularJs when you are sending values to view (UI) as below

{{ user.creation_time | date:'medium' }}

You will get the correct date in the UI.

Harshit
  • 109
  • 5
  • 19
1

var arr = ["jan","feb","march","april","may","june","july","aug","sep","oct","nov","dec"];

var d = new Date();

var month = arr[d.getMonth()];
var day   = d.getDate();

var year  = d.getFullYear();


console.log(day +"-" + month + "-" + year);


var hour = d.getHours();
var min = d.getMinutes();

var temp = "AM";

if(parseInt(hour)  > 12 ){
  hour = hour % 12 ;
  temp = "PM"; 
}

console.log(hour + ":" + min + " " + temp);
Shubham
  • 1,755
  • 3
  • 17
  • 33
1

This will help you:

var month = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];

var date = new Date();
var monthIndex = date.getMonth() + 1;
var minute = date.getMinutes() > 9 ? date.getMinutes() : "0" + date.getMinutes();
var hour = date.getHours();
var amPm = "am";

if (date.getHours() == 12) {
    amPm = "pm";
}

if (date.getHours() > 12) {
    hour = date.getHours() % 12;
    amPm = "pm";
}

var time = hour + ":" + minute + amPm;
/* getting time*/
var fullDay = date.getDate() + " " + month[monthIndex - 1] + " " + date.getFullYear()
Shashank Agrawal
  • 25,161
  • 11
  • 89
  • 121
Muhammad Waqas
  • 1,140
  • 12
  • 20
0
<script>
  angular.module('app', [])
    .controller('testCtrl', ['$scope', function($scope){
      var months =  ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
      var d = "2015-12-16";
      var date = new Date(d);
      var day   = date.getDate();
      var year  = date.getFullYear();


      console.log(day +"-" + months[date.getMonth()] + "-" + year);
    }])
</script>

plunker code here

Durgpal Singh
  • 11,481
  • 4
  • 37
  • 49