How to check for "Some Date" older than "Current Date"?
#1

Like for example, I have a date: "13th March 2011, 22:34.5" and today's date is "22 March 2012 3:17.16"

How can I compare the dates and check whether the date is older or newer?
Reply
#2

Well best thing you can do is: use getdate(); for this.
So you save for example:
Код:
new date1;
date1 = getdate();
and next week you check
Код:
new date2;
date2 = getdate();
if(date2 > date1) //then is date1 older .
Moreinfo:
https://sampwiki.blast.hk/wiki/Getdate
Reply
#3

compare each value in a decreasing order: year, month, day, hour, minute, second. when any comparison returns a difference, the date is (depends on how you check it, > or <) bigger.
are all date strings formatted the same way? yours looks like
pawn Код:
format(datestring,sizeof(datestring),"%dth %s %d, %d:%d,%d",day,monthname[month],year,hour,minute,second);
so by using
pawn Код:
sscanf(datestring,"d' 's[16]' 'd','d':'d'.'d",day,month,year,hour,minute,second)
and reordering the comparisons (years before months), it should work
Reply
#4

Quote:
Originally Posted by Babul
Посмотреть сообщение
compare each value in a decreasing order: year, month, day, hour, minute, second. when any comparison returns a difference, the date is different. are all date strings formatted the same way? yours looks like
pawn Код:
format(datestring,sizeof(datestring),"%dth %s %d, %d:%d,%d",day,monthname[month],year,hour,minute,second);
so by using
pawn Код:
sscanf(datestring,"d' 's[16]' 'd','d':'d'.'d",day,month,year,hour,minute,second)
and reordering the comparisons (years before months), it should work
Thats also possible, but it's much more work then the way I explain. But yea if you wan't you can use babul's way.
Reply
#5

one issue using my method, is the need of converting the month string into a number ("march"=3) for easier comparing, which is not that easy for strings. if all date informations are stored as numerical values, its a piece of cake
Reply
#6

Yep, but you can store all date informations in numerical right?
Only if a player has to input the date by hisself you cannot use my option.
Anyway we will see what he uses :P
Reply
#7

mktime: Get the timestamp from date.

getdate: Get the actual timestamp

Due to timezone, maybe is need to decrease/increase the mktime timestamp in some hours.
Reply
#8

Be aware that on the 31th of December 'getdate()' returns something like 365, but on the 1st of January, it returns 0 or 1 again. So it's safe, as long as the dates aren't around New Year. Maybe it's useful to compare the years too.
Reply
#9

I made a fairly decent guide explaining Unix Timestamps a while ago, this might not be what you want, but this is the most effective way of dealing with dates.
Reply
#10

Thanks everyone, all your information were useful. I've overcome this problem.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)