I have an api returning timestamp variable. When I print in AngularJs it shows as 1488195848000.But I want that to be displayed as 2017-02-28 14:49:48(In Timestamp format)
How do I convert in controller?
If you want to convert that variable in HTML,
{{variable | date : 'yyyy-MM-dd HH:mm:ss'}}
And in case of JS,
$filter('date')(variable, 'yyyy-MM-dd HH:mm:ss')
Note: In case of JS, do not forget to add $filter as dependency.
You can format date in AngularJS using the pipe operator and the date filter
<span>{{1488195848000| date:'yyyy-MM-dd HH:mm:ss'}}</span>
In the controller you need to make use of filter function to format the date
like
var _date = $filter('date')(_date, 'yyyy-MM-dd HH:mm:ss')
Related
I'm not so sure what's the proper way to insert date into the database, but I'm using new Date().
So I get date format like this when I query from the database:
2021-09-24T12:38:54.656Z
Now I realized that date format is not so user-friendly. So I'm trying to convert it if possible standard readable format like this:
Sept 25 2015, 8:00 PM
I tried using toLocaleString() to my date pulled from db but it won't work probably if I got it correctly pulled date from the db is already a string?
Is there a workaround for this so that I don't need to change how I enter date to my db?
const date = moment("2021-09-24T12:38:54.656Z").format('MMM D YYYY, h:mm a')
console.log(date)
<script src="https://momentjs.com/downloads/moment.min.js"></script>
use momentjs
You can find format method and apply as you desire
https://momentjs.com/
I think it is better to store in DB as UTC 00 value and you are doing it right now.
When you retrieve it, you will be getting a string value something like, 2021-09-24T12:38:54.656Z. On the UI you can easily convert it to date variable in JS using,
const dateVar = new Date("2021-09-24T12:38:54.656Z");
console.log(dateVar.toLocaleString());
If you want more date formatting other than the inbuild solutions like toLocaleString you can use date libraries like moment.js
You'd need to create a Date object from the timestamp string like this.
let date = new Date('2021-09-24T12:38:54.656Z');
Then you can use toLocaleString, toLocaleDateString, toDateString, or toString as you see fit.
I'm trying to convert below-mentioned date to ISO format(MongoDB)
var d = { storedDate: '26/06/2020 05:55:29 PM' };
I'm however unable to find the parameter that I need to use to get it in the format which I want. I tried the below piece of code.
moment(d.storedDate).format("YYYY-MM-DD HH:mm Z");
How can I get it as ISODate("2020-06-26T17:55:29.274Z")
Please advice
moment(d.storedDate, 'DD/MM/YYYY HH:mm:ss').toISOString() will return ISO date only in UTC that you need for MongoDb. You need to provide the input date format as well.
If you want to store proper Date object, use
moment(d.storedDate, 'DD/MM/YYYY HH:mm:ss').toDate()
You should not store date/time values as strings, use proper data type.
I want to show in a view of an angular project a human friendly date format, and I saw that angular has a filter to do so, but the input date needs to have a certain format.
The desired output format to be seen in the view is the following: "dd/MMMM/yyyy hh:mm:ss"
Currently, I have on a Database the following timestamp format:
"2016-08-15 12:34:34"
How can I format this type of timestamp so that angular can intepret it and format it as desired?
Thanks a lot!
You can parse the date to a javascript date and then use the date filter in Angular to show it in the format you want (if you can't use the date filter on the date from your database directly).
You could do this as a filter to make it easier:
myApp.filter('formatted', function() {
return function(value) {
return new Date(value);
}
});
In your html you could then add this filter before the date filter:
Filter: {{ vm.dateToFormat | formatted | date: 'dd/MMMM/yyyy hh:mm:ss' }}
If Angular is having trouble parsing the string from the database on its own you could just use JavaScript Date.parse("2016-08-15 12:34:34") to return you a date that Angular will interpret properly.
You could just use the inline filter:
Example:
<span>{{vm.Date | date: 'dd/MMMM/yyyy hh:mm:ss'}}</span>
EDIT: To be clear, you post doesn't clarify if that timestamp is a string or not, so you may have to parse it accordingly. I see the other posters just assumed it was a string.
I have a date format in the following way,
"tranDate": "2015-11-02T18:30:00.000Z"
how do i format the date and time in human readable way.
date: 2015-11-02
time: 18:30:00
the date filter will handle the UTC datetime string as it is.
Since your string contains Zulu time, first convert the string to a date using new Date as follows:
$scope.date = new Date(obj['tranDate'].replace(' ', 'T'))
Then use angular's inbuilt date filters. in the template. Example:
{{date | date: 'd MMM yyyy'}}
IN CONCROLLER
put the following code in controller
$scope.date_test = {"tranDate": new Date()}
IN HTML
date : {{date_test.tranDate | date: 'yyyy-M-dd'}}
time : {{date_test.tranDate | date:"h:mm:ss"}}
This is an exmaple of formating the date and time
http://plnkr.co/edit/nYChVvYOmok4jX16CNae?p=preview
I need to show date in the same timezone as it coming in from service. But angular date filter converts the date to local timezone. is there a way to avoid conversion in local timezone.
My input is 2014-12-11T05:21:22.323-05:00
required output - 12/11/2014 05:21:22 PM (without converting to local timezone)
Thanks in advance.
You can create a custom filter for this, just convert the desired date without the timezone, for example:
myApp.filter('ignoreTimeZone', function(){
return function(val) {
var newDate = new Date(val.replace('T', ' ').slice(0, -6));
return newDate;
};
});
Check this fiddle:
http://jsfiddle.net/ghxd6nom/
You can also use a library for this, like moment.js
Angular provides service '$filter'. We can inject this service on controller.
with the help of this service, we can format the date .
var dateFrom = $filter('date')(new Date(), "MM/dd/yyyy");
in this way, we can able to format the date with or without Timezone.
The return valus is string and we can convert the string into DateTime on Backend.
The otherway is to use library moment.js.
This library provides better way of converting/formatting Dates.
check this blog , which provides angular date/time filtering & formatting.
You can use this :
$filter('date')(date, format, '+0000')
Documentation: https://docs.angularjs.org/api/ng/filter/date