Unable to change date format in Angular - angularjs

I am new for Angular. I want to change date format that is sent from datepicker input.
Here is my code,
$scope.savePlaylist = function(s){
//s= 02-November-2015
var startDate = $filter('date')(s,'yyyy-MM-dd 23:59:59'); //it return 02-November-2015
var test = $filter('date')(new Date(),'yyyy-MM-dd 23:59:59');//it works!
};
So, I tried angular-moment,
$scope.savePlaylist = function(s){
//s= 02-November-2015
var startDate = $filter('amDateFormat')(s,'YYYY-MM-DD 23:59:59'); //it return empty
var test = $filter('amDateFormat')(new Date(),'YYYY-MM-DD 23:59:59');//it works!
};
I really need help. Thanks.

Try to assign date("s") to new date object and pass it to $filter. That will solve your problem.
$scope.savePlaylist = function(s){
//s= 02-November-2015
var dt = new Date(s);
var startDate = $filter('date')(dt,'yyyy-MM-dd 23:59:59'); //it return 02-November-2015
var test = $filter('date')(new Date(),'yyyy-MM-dd 23:59:59');//it works!
};

It cannot use 02-November-2015 to convert to other format. I changed it into
dd MMMM yyyy
It can work now. Thanks.

Related

How to get specific month on AngularJS

How can I get the specific date month coming from the backend on angularJS
I wish to add something like this:
var curMonth = new Date().getMonth();
var monthData = vm.paymentsData[0].date.Date().getMonth();
if (curMonth == monthData) {
console.log ("Same Month");
}
Im getting error on:
var monthData = vm.paymentsData[0].date.Date().getMonth();
it says:
angular.js:14328 TypeError: vm.paymentsData[0].date.Date is not a function
Thanks
Data from the backend
I think your code should be written as follows:
var curMonth = new Date().getMonth();
// this maybe a string .. and you cannot call Date() function in that way.
var monthData = vm.paymentsData[0].date;
// Date function should be called this way.
var monData = (new Date(monthData)).getMonth();
if (curMonth == monData) {
console.log ("Same Month");
}

age calculation in breezingform

I use Breezingforms(joomla extensions)
I have two field.
1- birthdate(persian calendar-jalali)
2-calculated_age -> textfield readonly for show Differs date.
this my code:
function showage(){
var one_year=1000*60*60*24*365;
var date1 = new Date();
var dob= ff_getElementByName('birthdate').value;
var tmp=ff_getElementByName('birthdate').value.split("/");
var date2=new Date(tmp[2],tmp[1]-1,tmp[0]);
var pattern = /[0-3][0-9]\/(0|1)[0-9]\/(19|20)[0-9]{2}/; //Regex to validate date format (dd/mm/yyyy)
if (pattern.test(dob)) {
var y1 = date1.getTime(); //getting current year
var y2 = date2.getTime(); //getting dob year
var diff=Math.floor((y1-y2)/one_year);
ff_getElementByName('calculated_age').value=diff;
}
}
I have a problem.I use var date1 = new Date(); for get current date.but this var output not Jalali date(persian date)
how solve it?

React native date convert from int to date

I am new in react-native. I want to convert an int date to a formatted date.
The problem is it's always displaying the current date and not that date from the following integer: 1528101680
This is what I've tried so far:
var newDate = moment(Date(1528101680)).format('MM/DD/YYYY hh:MM');
console.log(newDate);
You need to provide time in milliseconds
var newDate = moment(new Date(1528101680 * 1000)).format('MM/DD/YYYY hh:MM');
// -----------^^^^^^^------------
console.log(newDate);
var newDate = moment(new Date(1528101680 * 1000)).format('MM/DD/YYYY hh:MM');
console.log(newDate);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.js"></script>
try this :
var newDate = moment(1528101680 * 1000).format("MM/DD/YYYY hh:MM")
console.log(newDate)
var newDate = moment(1528101680 * 1000).format("MM/DD/YYYY hh:MM")
console.log(newDate)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.js"></script>

How to convert readable date time in timestamp which comes from backend

I want to convert the commit date and time to time stamp which I get from my APIs.
But I don't know how to do this in angular?
Here is my controller code :
var commitDate = item.commitMetaData.commitDate;
var dat = new Date(commitDate);
But it says "Invalid Date"
PS: Thanks in advance
What you could do is generate the date from the values montValue , year, dayOfMonth
with plain Javascript you could just do
var d = new Date();
d.setMonth(commitDate.monthValue +1); //see explanation below
d.setDate(commitDate.dayOfMonth);
d.setYear(commitDate.year);
be careful the months start at 0 so January is actually 0 so in your example you would have to add +1
You can also create a filter for this
.filter('createDate', function ($filter) {
return function (input) {
if (input != null && input != undefined) {
var d = new Date();
d.setMonth(input.monthValue +1); //see explanation below
d.setDate(input.dayOfMonth);
d.setYear(input.year);
return d;
}
};
})
and call it like
var commitDate = item.commitMetaData.commitDate;
var dat = $filter('createDate')(commitDate);
reference JS Date

how to display Json Formated Datetime in angularJs

how to display Json datetime formate in angularJs its showing Datetime as"/Date(820434600000)/"
Angular Code
app.controller("MyDeptCntrl", function ($scope, MyDeptSer) {
$scope.BtnDept = function () {
var Dept = MyDeptSer.GetDeptData();
Dept.then(function (d) {
$scope.DeptData = d.data;
// $filter('date')(date, format, timezone)
},function(e){
alert('Loading Failed....')
})
}
use below function to parse the date first
function dateParser(input){
input = input.replace('/Date(', '');
return new Date(parseInt(input,10));
}
so
$scope.DeptData = dateParser(d.data);
You can try this
convertJsonDateTimeToJs: function (jsonDate) {
var dateSlice = jsonDate.slice(6, 24);
var milliseconds = parseInt(dateSlice);
return new Date(milliseconds);
}
I'd recommend changing your server side code to use a friendlier JSON serializer but if not, try:
// You start with /Date(820434600000)/
// substr(6) takes off the /Date( part and pass the number 820434600000 into the parseInt function.
// Note: the parseInt function will ignore the )/ and the end.
var friendlyDate = new Date(parseInt($scope.DeptData.someDateMember.substr(6)));
// Then, you can format it using angular date filter -- for example:
$scope.formattedDate = $filter('date')(friendlyDate, 'MM/dd/yyyy');

Resources