How to calculate exact year, month and day between two dates in vc++/MFC - visual-c++

In my application i have to find out exact year, month and day between two dates. I tried with below snip. but answer is not correct. I used CTime for from to to dates. The result is obtained in CTimeSpan class. CTimeSpan has member function such as GetDays. We shall find out year, month and day from Total Days, but answer is not correct. Is any way to do ?
Thanks in advance

There is no common function to do that.
My idea would be:
COleDateTime date1(1979,12,31,0,0,0),
date2(2004,10,01,0,0,0);
// Get the normal differences
int iYears = date2.GetYear()-date1.GetYear(),
iMonths = date2.GetMonth()-date1.GetMonth(),
iDays = date2.GetDay()-date1.GetDay();
// Problematic underflow of days.
if (iDays<0)
{
// One month less
--iMonths;
// Advance from the start date until we reach the 1st of next month
for (iDays=0; (date1+COleDateTimeSpan(iDays,0,0,0)).GetDay()!=1; ++iDays)
;
// Now get the days from the 1st of the second date to the desired date.
iDays += static_cast<int>((COleDateTime(date2.GetYear(),date2.GetMonth(),1,0,0,0)-date2).GetTotalDays());
}
// underflow of months
if (iMonths<0)
{
--iYears;
iMonths +=12;
}

I don't see any CTimeSpan::GetTotalDays() function in the MSDN Docs.
Anyway, the GetTotalHours / GetTotalMinutes functions return the number of Complete Hours/Minutes and does not fully represent the TimeSpan value; the best way would be to use GetTotalSeconds and convert it as necessary.

Related

Nodejs change time at date type after getting from Mongodb

I wanted to change time at date type which returning from mongodb with custom time like below
"2021-05-26T00:00:00.000Z"
to
"2021-05-26T10:20:00.000Z"
I wanted to change time from a variable at the date, so my technique was split this date with "T" then get time part and change it with custom time
let splitedTime = timev[0].validFrom.toString().split()[0];
let customTime = "10:20:00.000Z";
let finalTime = splitedTime + customTime;
but this split not working this giving me date like this "Wed May 26 2021 06:00:00 GM". Can you please help me for this?
Working with Date
Whilst I understand your logic of converting it to a string and then using string methods to convert it to your desired output, I believe a simpler approach is to use the Date object
function dateAdd(original, hours, minutes) {
const date = new Date(original);
date.setHours(original.getHours() + hours);
date.setMinutes(original.getMinutes() + minutes);
return date.toISOString();
}
When original = "2021-05-26T00:00:00.000Z" then the return value is "2021-05-26T10:20:00.000Z".
If you want a fixed time:
const date = new Date('2021-05-26T00:00:00.000Z');
date.setUTCHours(10);
date.setUTCMinutes(20);
date.setUTCSeconds(0);
date.setUTCMilliseconds(0);
// a cleaner approach:
date.setUTCHours(10, 20, 0); // hoursValue, minutesValue, secondsValue
console.log(date.toISOString());
Which produces the following:
"2021-05-26T10:20:00.000Z"
Another Solution
Your actual problem is being caused by the fact you call toString which returns a date string in the format of "Tue Aug 19 1975 23:15:30 GMT+0200 (CEST)" so when you're splitting by "T", that's way down at the end. toISOString will return the correct format.
Explanation
As you can see above, we avoid using string methods and use the methods that exist on Date. This approach is safer as you avoid issues with the difference between toISOString and toString. You may also find moment useful if you're using dynamic methods of changing dates regularly.
Note
In all honesty, I'm not entirely sure I understand the why behind what you're doing, so if I'm wrong please correct me so I can update my answer to be more relevant for you.
Learn More
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toString
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

Why does new Date(year,month,day) not return an equivialent Date?

one day I fiddled with vanilla NodeJS using the node command line tool. (I am using node v13.11.0)
I tried to create a new Date at the 01.01.1970. I used the usual new Date(year, month, day) constructor.
As simple as it sounds, I entered new Date(1970, 1, 1) and found out, that it does not return 1970-01-01T00:00:00.0000Z. Instead, it returns 1970-01-31T12:00:00.000Z.
Has anyone an Idea, why this constructor does not return the equivalent date?
The constructor does more or less what you think:
x = new Date(1970,1,1)
1970-01-31T14:00:00.000Z
> x.getMonth()
1
> x.getDate()
1
> x.getHours()
0
(Note that months count from zero, so you requested the 1st of February).
But if you display the whole date as a string, it's showing the time in UTC, which might not be what you expect.

Moment.js sets dates to 1 day behind

I am using 2.22.1 to format dates.
When i put in a date that comes from a date selector, moment will put the date back one day. For example, when I try to format a date in the following way:
Example 1:
const day = new Date(value).getDate();
const month = new Date(value).getMonth();
const fullYear = new Date(value).getFullYear();
console.log('day', day); // 7
console.log('month', month); // 5
console.log('fullYear', fullYear); //2018
Formatting function:
moment.utc(new Date(month + ' ' + day + ' ' + fullYear), 'MM-DD-YYYY').format('DD-MM-YY')
Output: 06-05-18
Expected: 07-05-18
Example 2:
Input: Thu May 31 2018 00:00:00 GMT+0100 (GMT Summer Time)
Formatting function:
moment.utc(new Date(value)).format('DD-MM-YY')
Output: 30-05-18
Expected: 31-05-18
moment.utc will convert the input to universal time.
You are running new Date(...) with an input that's based on GMT +1.
If you think about this, it makes total sense.
Your output is 30-05-18, because it's 11 PM / 23:00 o'clock on the previous day.
This (how ever) would in fact work:
moment('05-06-2018', 'MM-DD-YYYY').format('DD-MM-YY')
// alternatively (and preferrably) this:
moment.utc('05-06-2018', 'MM-DD-YYYY').format('DD-MM-YY')
and output: "06-05-18"
Because the non utc version does not take a time input in this example.
One of the reasons moment.js exists, is to get rid of Date in your code all together.
(Keep in mind tho, that Date has drastically improved now. DateTimeFormat is a game changer)
Please just read the momentjs documentation on how to properly use moment.
edit:
If you want to process 400000 dates with this, I'd advise using RegExp, .split, .exec, .slice or Date instead.
(I can relate since I wrote a client sided Log parser with javascript generators and Service Workers for a statistical anti-cheat analysis)
I truly recommend playing around with such things to raise your knowledge.
I just ran into this issue and a quick fix I found for the time being processed in "Zulu time" (due to the Z at the end of the string) is to add .LocaleString() after the date variable.
I find that for data consistency, it's easier to store data in UTC time and then convert it to the locale string when displaying the data to the user.
For example, I'm using:
moment.utc(dateVariable.toLocaleString()).format("MM/DD/YYYY")

Manipulation of date in Groovy language

I have this script in Groovy:
currentDate = new Date().format( 'yyyyMMdd' )
I want to be able to manipulate the date in order to 'play' with the dates of it..
for example if I have this:20150701 I want to subtract days, weeks or months for example if I subtract one day it will be 20150630.
How can I do it without using TimeCategory?
thanks!
Without TimeCategory, you can only add or subtract days. If you want to add/subtract other fields, TimeCategory is a good way to go.
If your annoyance with TimeCategory is the with syntax, one alternative would be to use mixins (although they are generally considered deprecated since traits have been added to Groovy):
[Date, Integer].each { it.mixin(groovy.time.TimeCategory) }
def lastMonth = new Date() - 1.months
OK I have the answer to it, I tried this and it worked:
currentDate = new Date()
def yesterday = currentDate - 1
currentDate = yesterday.format("yyyyMMdd")

Getting last day of current month

In Groovy, how can I find the last day of the current month?
Thanks for your sage advice and better wisdom.
Yeah, as you commented, Calendar.FIELD is probably your best friend (though groovy's TimeCategory also has neat tricks).
Here's how i made for the last day of the current month:
Calendar.with {
println instance.getActualMaximum( DATE )
}
I didn't understood what you meant by "break out the current weekday if I'm looping through a month". Perhaps getting the weekday for each month?
Calendar.with {
(JANUARY..DECEMBER).each { month ->
def cal = instance
cal[MONTH] = month
println cal[DAY_OF_WEEK]
}
}

Resources