Customize calendar function in angularMoment - angularjs

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

Related

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

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

MomentJS invalid date

I'm trying to convert a date string like 'DD-MM-YYYY', to 'YYYY-MM-DD'.
Unfortunately I'm getting a invalid date.
var startDate = $('.startDate').val();
var endDate = $('.endDate').val();
var newstartDate = moment(startDate).format( "YYYY-MM-DD");
endDate = moment(endDate, "YYYY-MM-DD");
console.log('startDate:',startDate)
console.log('newstartDate:',newstartDate)
console.log('endDate:',endDate)
How could I get the output as: 2018-09-14 . / 2018-08-14 ?
var startDate = "22-12-2009";
var endDate = "01-02-2009";
var newstartDate = moment(startDate,'DD-MM-YYYY').format( "YYYY-MM-DD");
endDate = moment(endDate, 'DD-MM-YYYY');
console.log('startDate:',startDate)
console.log('newstartDate:',newstartDate)
console.log('endDate:',endDate)
<script src="https://momentjs.com/downloads/moment.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Specify the input date format when parsing with moment moment(startDate,'DD-MM-YYYY').format( "YYYY-MM-DD");

compare jalali dates in cakephp

I need to get users which their payment_time is between startDate and endDate in cakePHP.dates are in jalali format :
۱۳۹۷ - ۰۱ - ۱۴
and my controller is as follows:
public function search(){
if ($this->request->is('post')){
if(!empty($this->request->data['User']['start_date'])&&
!empty($this->request->data['User']['end_date'])){
$start_date = $this->request->data['User']['start_date'] ;
$end_date = $this->request->data['User']['end_date'];
$this->Paginator->settings['conditions']['AND'] = array(
'User.payment_time >= ' => $start_date,
'User.payment_time <= ' => $end_date);
$this->set('users', $this->Paginator->paginate());
}
}
But users is view page is null. Is this method can help for jalali dates?
Simply make a snippet to replace the Urdu/English numerals with English/Urdu numerals then parse the dates.

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

Change date format in angularjs

I am getting date format like this /Date(1497683045200)/. I want to convert it as dd/MM/yyyy. I tried this
<td>{{data.From | date:'MM/dd/yyyy' }}</td>
But format is not changed.Any suggestion?
create a custom filter like this
.filter('jsonDate',function(){
return function(date){
return new Date(date.match(/\d+/)[0] * 1);
}
})
call it like this
<td>{{data.From | jsonDate | date:'MM/dd/yyyy'}}</td>
try this
formatDate(date) {
let d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
if (month.length < 2) month = '0' + month;
if (day.length < 2) day = '0' + day;
return [ month, day, year].join('/');
}
Call that custom method on HTML
<td>{{formatDate(data.From)}}</td>

Resources