React native date convert from int to date - reactjs

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>

Related

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?

Convert datetime to timestamp milliseconds adding +1 month

high chart not rendering in IE and Safari, and solution for this problem is converting my date from API to time stamp in milliseconds.
here is code for convert
var date = '2017-06-07 10:00:00'
var d = date.match(/(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/)
console.log(d)
var parsed = +(new Date(d[1], d[2], d[3], d[4], d[5], d[6]).getTime())
console.log("timestamp: " + parsed) // 1499414400000 ==> July 7, 2017 8:00:00 AM
But I allways get +1 month
here is example
js fiddle
This is because the moth count start with zero you can see here the full explanation:
https://www.w3schools.com/jsref/jsref_getmonth.asp
Hello Please check it out this can be your perfect solution
const date = '2017-06-07 10:00:00'
const d = date.match(/(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/)
var dates = new Date(d[0])
const parsed = new Date(dates).getTime()
Highcharts.stockChart('container', {
series: [{
data: [[parsed, 10]]
}]
});
thnx to all, here is how I fix this.
var date = '2017-06-07 10:00:00'
var d = date.match(/(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/)
var parsed = new Date(d[1], d[2]-1, d[3], d[4], d[5], d[6]).getTime() //I added -1 for month
Highcharts.stockChart('container', {
series: [{
data: [[parsed, 10]]
}]
});

Display all days(dates) between two dates in angularjs

I want to use select "From" and "To" dates from datepicker and display all dates in between, in tabular form using angularjs.
Try this function to calculate no. of days. Hope it helps
function daysCalculator(date1, date2) {
var d1 = new Date(date1);
var d2 = new Date(date2);
var days = d1.getTime() - d2.getTime();
days = parseInt((((days / 1000) / 60) / 60) / 24)
return days;
}
Try using this.
function getDates(fromDate, toDate) {
var dateArray = [];
var nextDate = fromDate;
while (nextDate <= toDate) {
dateArray.push( nextDate );
nextDate.setDate(fromDate.getDate() + 1);
}
return dateArray;
}

momentjs returns error "lastseen.diff is not a function"

I am comparing date with the help of Momentjs in Angular.
curdate = moment();
curdate = curdate.format('DD-MM-YYYY');
lastseen = moment(data.last_seen); //this data is coming from database (DD-MM-YYYY)
lastseen = lastseen.isValid();
duration = lastseen.diff(curdate, 'days') ;
alert(lastseen); // it is returning false.
alert(duration);
In Firebug I am getting below error.
Error: lastseen.diff is not a function
Object { _i: "09-02-2017", _f: undefined, _l: undefined, _isUTC: false, _d: Invalid Date }
What am I doing wrong?
you can skip this line lastseen = lastseen.isValid(); because when you do this.. last seen become a boolean value i.e true or false. this value does not have a function diff.
curdate = moment();
// skip this like.. use moment object
// curdate = curdate.format('DD-MM-YYYY');
lastseen = moment("24-02-2017",'DD-MM-YYYY'); //this data is coming from database (DD-MM-YYYY)
//skip this also
//lastseen = lastseen.isValid();
duration = lastseen.diff(curdate, 'days') ;
alert(lastseen); // it is returning false.
alert(duration);
<script src="http://momentjs.com/downloads/moment.js"></script>
Replace this
lastseen = moment(data.last_seen);
with
lastseen = moment(data.last_seen, "MM-DD-YYYY");
and DO NOT replace lastseen with a boolean
lastseen = lastseen.isValid();
For more details on moment.js Difference
var app = angular.module("app", []);
app.controller("ctrl", function($scope) {
var curdate = moment();
var lastseen = moment('12-02-2017','DD-MM-YYYY'); //this data is coming from database (DD-MM-YYYY)
var flag = lastseen.isValid();
var duration = curdate.diff(lastseen, 'days');
alert(flag); // it is returning false.
//Adding plus to include start date, If you want to include
alert(duration+1);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
{{resultados}}
</div>

Unable to change date format in Angular

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.

Resources