Getting only "today and yseterday" from ngx-moment in angular 9 - angularjs

I'm using ngx-moment pipe to display my date
it's really helpful and very useful
But I want to display the date from today and tomorrow and then the calendar date like that
if the date today then "Today and 00:00pm", it tomorrow then "Yesterday at 00:00pm",
Then if the day after tomorrow I need to show the calendar date "13/9/2020"
here is the code
date:'2020-09-13T00:00:00' ;
<div>Last updated: {{date| amTimeAgo}}</div>

I solved it
moment.updateLocale('en', {
calendar : {
lastDay : '[Yesterday] LT',
sameDay : '[Today] LT',
nextDay : 'LL',
lastWeek : 'LL',
nextWeek : 'LL',
sameElse : 'LL'
}
});

Related

How to convert string to date in react native with moment.js?

I have this date in string, now i want to convert this with UTC using moment.js.
Wednesday, 22 September 2021, 7:00:00 am
Expected result:
Formate: DD-MMM-YYYY hh:mm a
Date: 22-Sep-2021 12:30 PM
I tried this way but not working
let dateStr = 'Wednesday, 22 September 2021, 7:00:00 am'
utcDate = moment(moment(dateStr, 'DD-MMM-YYYY hh:mm a'))
.utc()
.format('DD MMM YYYY, hh:mm A');
Also I tried this conversion with dayjs but it works well in iOS but not in Android where I get null.
utcDate = dayjs(new Date(`${dateStr} UTC`)).format(
'DD MMM YYYY, hh:mm A',
);
let me know if anyone have solution for this.
Thanks!
Try this:
let dateStr = 'Wednesday, 22 September 2021, 7:00:00 am'
const utcDate = moment(new Date(dateStr))
.utc()
.format('DD MMM YYYY, hh:mm A');
console.log(utcDate)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js" integrity="sha512-qTXRIMyZIFb8iQcfjXWCO8+M5Tbc38Qi5WzdPOYZHIlZpzBHG3L3by84BBBOiRGiEb7KKtAOAs5qYdUiZiQNNQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
You need to remove the format from moment call.
You can do this but it is a deprecated way
let dateStr = 'Wednesday, 22 September 2021, 7:00:00 am'
// You can do this but it is deprecated way
const date = moment(dateStr)
.utc()
.format('DD MMM YYYY, hh:mm A');
console.log(date)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
The better solution.
let dateStr = 'Wednesday, 22 September 2021, 7:00:00 am'
const date = moment(new Date(dateStr))
.utc()
.format('DD MMM YYYY, hh:mm A');
console.log(date)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>

Convert datetime format in string to date javascript?

Let say I have an object product
I can use product.createdDate to get 2018-04-16 15:12:46.179427 or any other date with the same format.
how do I convert to date in javascript so I and show it as 16 April 2018?
I'm using angularjs.
I tried {{product.createdDate | date : 'dd MM, yyyy'}}but it show 2018-04-16 15:12:46.179427
There is no straightforward way to do it. You can find your answer in a similar question here :
Get current date in DD-Mon-YYY format in JavaScript/Jquery
You can try the following:
var date = new Date("2018-04-16 15:12:46.179427".replace(/-/g,"/"));
var d = moment(date).format('D MMMM YYYY')
console.log(d);
<script src="https://rawgit.com/moment/moment/2.2.1/min/moment.min.js"></script>
You can use date pipe to filter it out
html
<p>
{{date | date:'dd MMMM yyyy'}}
</p>
In your controller
$scope.date = new Date("2018-04-16 15:12:46.179427");
To get directly in controller inject $filter
var date = $filter('date')(new Date("2018-04-16 15:12:46.179427"), 'dd MMMM yyyy');
console.log(date); // 16 April 2018
Please refer the demo link
To convert this date using javascript you have to define the months like bellow.
const months = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
];
const date = new Date('2018-04-16 15:12:46.179427');
const formatedDate = date.getDate() + ' ' + months[date.getMonth()] +' '+ date.getFullYear();
But if you only want to show it in the UI, use a pipe to convert it.

how to get hh:mm:ss in angularjs-datepicker

hi i am using angularjs datepicker functionality using npm i angularjs-datepicker working fine but i need to show time like(15-12-2017 05:14 PM) help how to format the datepicker here i mentioned reference link also i tired from controller it's came but when i click date picker date shown wrongly .there is any specify format is there help or how to solve that problem
https://www.npmjs.com/package/angularjs-datepicker
html
<div class="hackyhack" datepicker datepicker-class="test-custom-class"
date-format="dd-MM-y" >
<input type="text" id="AddQuoteDate" name="QuoteDate" size="4"
ng-model="QuoteDate" placeholder="Enter Quote Date"
ng-change="QuoteDatechange(QuoteDate)"
class="form-control angular-datepicker-input"
required title="Enter Quote Date"/>
</div>
Controller
$scope.QuoteDatechange = function (Quotedate) {
var date = new Date();
var hours = date.getHours() > 12 ? date.getHours() - 12 : date.getHours();
var am_pm = date.getHours() >= 12 ? "PM" : "AM";
hours = hours < 10 ? "0" + hours : hours;
var minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
var seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
time = hours + ":" + minutes + ":" + seconds + " " + am_pm;
$scope.QuoteDate = Quotedate + " " + time;
}
According to the DOCs you have to use the AngularJs date format
Option: date-format=""
Type: String
Default: String(new Date())
Description: Set the date format you want to use, see the list here
<datepicker date-format="dd-MM-yyyy hh:mm a" selector="form-control">
<input type="text" ng-model="QuoteDate"/>
</datepicker>
It's Working for me. Try this. Hope it will help you.
var currentdate = new Date();
var hours = currentdate.getHours();
var minutes = currentdate.getMinutes();
var ampm = hours >= 12 ? 'pm' : 'am';
hours = hours % 12;
hours = hours ? hours : 12; // the hour '0' should be '12'
minutes = minutes < 10 ? '0'+minutes : minutes;
var strTime = hours + ':' + minutes + ampm;
$scope.QuoteDate = pad(currentdate.getDate()) + "/"
+ pad(currentdate.getMonth()+1) + "/"
+ currentdate.getFullYear() + " "
+ strTime;
function pad(s) { return (s < 10) ? '0' + s : s; }
<input type="text" id="AddQuoteDate" name="QuoteDate" size="4" ng-model="QuoteDate" placeholder="Enter Quote Date" class="form-control angular-datepicker-input" required title="Enter Quote Date"/>
$('#AddQuoteDate').datetimepicker({
minDate:new Date(),
format: 'DD/MM/YYYY hh:mma'
});

how to resolve this interpolate error

My Code is working fine but console shows the error for interpolate:
Error: [$interpolate:interr] Can't interpolate: {{expression}} {{date_expression | date : 'mediumDate'}} TypeError: date is undefined
{{expression}}
giving its right result and
{{date_expression | date :'mediumDate' }}
shows correct date in the right format Sep 3, 2015
This is the method which I am using:-
$scope.formatDate = function(date){
// var date = '030915',
day = date.substring(0, 2),
month = date.substring(2, 4),
year = date.substring(4, 6);
var dateOut = new Date('20' + year, month - 1, day);
return dateOut;
}
<div> {{item.origin}} {{formatDate(item.departuredate) | date : 'mediumDate' }}
</div>
here item.departuredate = "030915".
It is working fine but shows interpolate error.
Thanks
but I want to remove this error from the console. Someone could help me to resolve this problem.
Thankyou

Customize calendar function in angularMoment

I would like to use customized output for calendar in angularMoment.
In momentjs I set it up with:
moment.lang('en', {
calendar : {
lastDay : '[Yesterday at] LT',
sameDay : '[Today at] LT',
nextDay : '[Tomorrow at] LT',
lastWeek : 'ddd, LT',
nextWeek : 'dddd [at] LT',
sameElse : function () {
if (this < moment().startOf('year'))
return 'M/D/YY';
else
return 'ddd, MMM D';
}
}
});
In angular, I tried the following (and a few variations) with no luck:
angular.module('main').constant('angularMomentConfig', {
lang: 'en'
, {
calendar : {
lastDay : '[Yesterday at] LT',
sameDay : '[Today at] LT',
nextDay : '[Tomorrow at] LT',
lastWeek : 'ddd, LT',
nextWeek : 'dddd [at] LT',
sameElse : function () {
if (moment() < moment().startOf('year'))
return 'M/D/YY';
else
return 'ddd, MMM D';
}
}
}
});
Apparently, you don't need to do anything different! I used the moment.lang definition without change and it works fine.
(Note: use moment.locale for more recent versions.)

Resources