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?

1

5 Answers 5

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'}}
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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);

Comments

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()

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.