NodaTime usage for datetimepicker - winforms

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

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.

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

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());

how to check datetime in angularjs?

posts = [{"content":"content1",
"created":"2013-12-27T14: 15: 27.747Z"},
{"content":"content2",
"created":"2013-12-27T14: 15: 02.956Z"}]
How to check posts[0] and posts[1] was created in the same day or not with Angularjs?
Angular doesn't really have a built in way to compare dates, but it can be done in conjunction with angular.
http://jsfiddle.net/TheSharpieOne/LxTGd/1/
This will compare if the day, month, and year are the same, determining if the 2 dates are on the same day (time is irrelevant).
$scope.sameDay = function(date1,date2){
return date1.substring(0,10) === date2.substring(0,10);
}
This functionality would ideally be made into some sort of directive. But this is just a basic example, a directive example can be made if required.
javascript cannot parse ISODATE which contain spaces:
post[0].created.replace(/\s/g,'')
That's how you compare dates:
(new Date(post[0].created).toDateString) === (new Date(post[1].created).toDateString)

Temporarily suspend data-binding in AngularJS to update model without firing $scope.$watch

UPDATE 4/19/2012 12PM PST: Gotta eat crow on this one. The problem was not with Angular's databinding but with math errors in how I was calculating the dates. I wasn't properly taking into account the minutes and seconds in my time calculations. I was just subtracting the timestamps from each other expecting the hours to come out nicely.
I've gotten myself into trouble with AngularJS databinding in which my different inputs need to be reciprocally bound to each other.
The form needs to contain the following:
A start date (this is a given, not an input)
An input to add hours on to the start date
Two inputs with the result: 1) a date picker and 2) an hour picker
If you change any of the inputs, it should update the others. So, the following would be desirable results:
(with original date-time as April 19th at 10pm). User enters '1 hour'. The result date becomes April 19th and the result time becomes 11pm.
Building on the above example, the user changes the date input to April 20th. The 'hours' now become 25 hours.
I've placed set up watchers, using $scope.$watch on each of these variables:
$scope.$watch('hours', function (newHours, oldHours, scope) {
if (newHours) {
var newEndDate = new Date(scope.origDate),
offHours = newEndDate.getHours();
newEndDate.setHours(offHours + newHours);
scope.endDate = newEndDate;
scope.endHours = newEndDate.getHours();
}
});
$scope.$watch('endHours', function (newEndHours, oldEndHours, scope) {
if (newEndHours) {
var newEndDate = new Date(scope.endDate);
newEndDate.setHours(newEndHours);
scope.endDate = newEndDate;
}
});
$scope.$watch('endDate', function (newEndDate, oldEndDate, scope) {
if (newEndDate) {
scope.hours = (newEndDate - scope.origDate) / (1000 * 60 * 60);
}
});
Each of these works fine on their own, but in tandem they cause a big fat mess. From my understanding of Angular, it seems that they're creating a 'feedback loop.' Edit: To wit, model 'A' will be updated and trigger a change on model 'B'. Model 'B' will then trigger a change on model 'A'.
Now, is it possible to temporarily suspend data-binding so that I can just update the models without firing the watchers? In some other contexts I've worked in (UITableView on Cocao comes to mind), one can ask to "stop updates," make some changes to the model, and then "resume updates."
Is there anything like this in AngularJS? If not, what am I not getting here, and how could I set up my project to achieve the desired functionality?
Here's a plunkr of my example.

Infragistics UltraCalendarCombo seems to be on drugs

I have an UltraCalendarCombo calendar control that I need to disable a set of dates in (weekends and public holidays basically).
I iterate through the list of dates for that month and set all the corresponding dates to disabled, this makes the current month look alright, but when I click the next month button on the control and try to access a month past now + 3 or 4 it jumps back to this month.
Has anyone any idea what this control is smoking, and where I can get some?
DateTimeCollection badDates = getMeSomeBadDatesmonth, year);
foreach (DateTime date in badDates)
{
myForm.CalendarInfo.DaysOfMonth[date.Day].Enabled = false;
}
Their controls have been going downhill for some time. I want to know what they are all smoking over there. I have been using their controls since the 2004 version of Net Advantage(which I really liked), but I have given up on using them on any new development. The bloat of features has broken what functionality did work and the documentation is just horrendous :(
Sorted it, the DaysOfMonth collection applies to a specific day (by number) of all months. For example, DaysOfMonth[10] applies to the tenth of every month of the year.
So I needed to use:
Infragistics.Win.UltraWinSchedule.Day theDay = _form.ValueDateCalInfo.GetDay(date, true);
if (theDay != null) theDay.Enabled = false;

Resources