how to get value from firebase as date while using moment.js - angularjs

I'm using firebase as database and momentjs for dates on my Ionic Project.
I save needed dates on firebase but i want to take 2 dates and find difference between them as hours, days etc.
But when i try to take this value's something goes wrong and can not take difference.
Fields on firebase.
My code.
let ms = moment(this.userActivityLoginTime,"DD/MM/YYYY HH:mm:ss")
.diff(moment(this.userActivityLogoutTime,"DD/MM/YYYY HH:mm:ss"));
let d = moment.duration(ms);
console.log(d.days(), d.hours(), d.minutes(), d.seconds());

Your code is wrong. You are trying to subtract bigger value from smaller one. Try code below.
let ms = moment(this.userActivityLogoutTime,"DD/MM/YYYY HH:mm:ss").diff(moment(this.userActivityLoginTime,"DD/MM/YYYY HH:mm:ss"));
let d = moment.duration(ms);
console.log(d.months(),d.days(), d.hours(), d.minutes(), d.seconds());

Related

How to get raw date string from date picker?

I'm struggling for hours with this seemingly trivial issue.
I have a antd datepicker on my page.
Whenever I choose a date, instead of giving me the date I chose, it gives me a messy moment object, which I can't figure out how to read.
All I want is that when I choose "2020-01-18", it should give me precisely this string that the user chose, regardless of timezone, preferably in ISO format.
This is not a multi-national website. I just need a plain vanilla date so I can send it to the server, store in db, whatever.
Here are some of my trials, so far no luck:
var fltval = e;
if (isMoment(fltval)) {
var dat = fltval.toDate();
//dat.setUTCHours(0)
fltval = dat.toISOString(); // fltval.toISOString(false)
var a = dat.toUTCString();
//var b = dat.toLocaleString()
}
It keeps on moving with a few hours, probably to compensate for some timezone bias
UPDATE 1:
the datestring is data-wise correct. But its not ISO, so I cant use it correctly. I might try to parse this, but I cannot find a way to parse a string to date with a specific format.
UPDATE 2:
I also tried adding the bias manually, but for some reason the bias is 0
var dat = pickerval.toDate()
var bias = Date.prototype.getTimezoneOffset()// this is 0...
var bias2 = dat.getTimezoneOffset()// and this too is 0
var d2 = new Date(dat.getTime()+bias)
var mystring= dat.toISOString() //still wrong
Thanks!
Javascript date functions can be used,
I assume you are getting in 2022-01-03T11:19:07.946Z format then
date.toISOString().slice(0, 10)
to 2022-01-03
There are 2 ways to get the date string:
Use the moment.format api:
date.format("yyyy-MM-DD")
Use the date string that is passed to the onChange as second parameter
Here is a Link.
I am assuming your code snippet is inside the onChange method. This gives you a moment and a date string to work with (the first and second parameters of the function respectively).
You have a few options. You could set the format prop on the DatePicker to match the format of the string you want. Then just use the date string. Or you can use the moment object as Domino987 described.

Unhandled Rejection (TypeError): formProps.date.getTime is not a function

How can I change hour and minute values to an existing Date variable?
formProps.date: existing Date type variable generated from a date picker that I want to use year value only.
formProps.hour: The hour value that user input separately.
formProps.minute: The minute value that user input separately.
Those three values are to be combined into a new Date variable 'dateWithTime', but it throws an error during copying the date values.
It this a wrong way to copy a Date variable? or is there any better way to make it?
const dateWithTime = new Date(formProps.date.getTime());
dateWithTime.setHours(formProps.hour, formProps.minute);
==== edit ====
the log of
console.log(formProps.date.toLocaleString());
console.log(typeof(formProps.date));
I don't know why your code above doesn't work but the below code worked for me. Could u try to use:
const dateWithTime = new Date(formProps.date.toLocaleString());
(I know it is not an comprehensive answer but I couldn't add a comment due to my reputation :/ )

NodaTime usage for datetimepicker

To avoid label of duplicate here's a brief summary of all what i did.
After spending hours of googling to calculate the difference between two dates I came across here and here where, i was convinced to use NodaTime to get difference in terms of years,months and days.My application needs accuracy to calculate pension.I used datetimepicker to get the date value from form and then i use Date.cs from here to extract the date in dd/mm/year and then insert it into database.To subtract the two dates using Period.Between(date1, date2, PeriodUnits.Years).Years how should i pass datetimepicker to it?
Here's what Jon Skeet said: "you can use LocalDateTime.FromDateTime and then use the Date property to get a LocalDate".
How should i get a complete rid of time while inserting in database as well as finding the difference while using datetimepicker instead of Datetime.
Update:
//Date of appointment
var d_app = LocalDateTime.FromDateTime(dateTimePicker1.Value).Date;
//Date of retirement
var d_ret = LocalDateTime.FromDateTime(dateTimePicker2.Value).Date;
var years=Period.Between(d_app,d_ret,PeriodUnits.Years).Years;
var months = Period.Between(d_app, d_ret, PeriodUnits.Months).Months;
var days = Period.Between(d_app, d_ret, PeriodUnits.Days).Days;
MessageBox.Show(years.ToString()+" years"+months.ToString()+"months "+days.ToString()+"days");
Giving the code datetimepicker1.value as 2/21/1990 (d_app) and datetimepicker2.value as 3/09/2015(d_ret) it returned 25 yrs 300months 9147
days.
What am i doing wrong?
You're performing three separate computations here. You only need one:
var appointment = LocalDateTime.FromDateTime(dateTimePicker1.Value).Date;
var retirement = LocalDateTime.FromDateTime(dateTimePicker2.Value).Date;
var difference = Period.Between(appointment, retirement);
MessageBox.Show(string.Format("{0} years {1} months {2} days",
difference.Years, difference.Months, difference.Days));

'date' filter without time zone in angularjs

This is input format:
yyyy:MM:dd'T'HH:mm:ss'Z' (Coming as a string from json service)
Required output format:
dd-mmm-yyyy
I have tried with {{txnDate | date:'dd-mm-yyyy'}}
but it is not working..
What is the format you are following for your date?
A quick var a = new Date(); a.toISOString(); in console will give you something like "2015-02-19T13:30:13.347Z". The formatted string you are receiving is not following any standard and I am afraid parsing it to date will result in Invalid Date in most of the browsers.
So you can either
Get your Date in proper format.
Make the best use of whatever is available. You can use split to break your string into individual components.
Something like:
var a = "yyyy:MM:dd'T'HH:mm:ss'Z'" //Replace with actual string
b=a.split(':') will result in ["yyyy", "MM", "dd'T'HH", "mm", "ss'Z'"] giving you year and months in b[0] and b[1].
For date, you can use b[2].substring(0,2) to give you dd.
You have all date components(apart from time components, which you don't need anyway) as string.
Either use them directly(as a string) or make a date object using these components(since you want month in MMM format).
$scope.txnDate = new Date(b[0]+'/'+b[1]+'/'+b[2].substring(0,2));
I am sure there are more ways to optimize this. Comment if this doesn't work for you, will try to elaborate more.

Angular UI datepicker giving me a UTC converted string. How to parse it back to local time accounting for the time difference?

I am currently running into issues with datepicker automatically converting my time to UTC. Is there anything I can pass into datepicker for it to not give me back a UTC converted string? For example the user picks March 19 on the calendar and the returned string would be something like this
'2014-03-19T04:00:00.000Z'
What I want is ->
'2014-03-19T00:00:00-04:00'
What I am trying now (sort of hacking around it) is trying to use moment js to convert it back to my desired (expected) format, but I am having trouble doing so without hardcoding a subtraction in there. I want to be able to convert it from UTC back to local time.
Does anyone know of a solution to this using moment js or angular?
I ran into the same problem and used a filter that adjusts the date and time using the local data to get around it:
filter('adjustDatepicker', ['$filter', function($filter){
var dateFilter = $filter('date');
return function(dateToFix){
var localDate, localTime, localOffset, adjustedDate;
localDate = new Date(dateToFix);
localTime = localDate.getTime();
localOffset = localDate.getTimezoneOffset() * 60000;
adjustedDate = new Date(localTime + localOffset);
return dateFilter(adjustedDate, 'MM/dd/yyyy');
};
}])
Use it like this in your template file:
{{details.datetomodify | adjustDatepicker}}
I think new Date('2014-03-19T04:00:00.000Z').toString() will give you the local version of the UTC time.

Resources