Skip to main content
Inspiring
July 7, 2023
Question

Date comparison for validation.

  • July 7, 2023
  • 1 reply
  • 587 views

Hi people, good afternoon!

I would like to know how I can be creating a code that expires within a time, using getDate and time.

For example, having an input field, where I check if a token exists, and compare the dates to check if it is active within the period.

 

(example):

new InitialDate: DD/MM/YY
new EndDate: DD/MM/YY

function ... {

if(code.text = "itsCode")

{

if(InitialDate > EndDate)

{

gotoAndStop(2);

}

else

{

trace("error, expired");

}

This topic has been closed for replies.

1 reply

kglad
Community Expert
Community Expert
July 7, 2023

use the getTime() method of the date object

vvvverTAuthor
Inspiring
July 10, 2023

Hi Kosglad, can't find a way to get getData to add to the variable.

I put together this code and I'm still wondering how I can add, in date_now, the current date of the user.

Can you help me?

 

var date_start:Date = new Date(2023, 6, 10);
var date_now:Date = new Date(); // HERE!
var differenceInMilliseconds:Number = date_end.time - date_start.time;

const MILLISECOND_PER_SECOND:int = 1000;
const SECOND_PER_MINUTES:int = 60;
const MINUTES_PER_HOUR:int = 60;
const HOURS_PER_DAY:int = 24;

var differenceInSeconds:Number = differenceInMilliseconds / MILLISECOND_PER_SECOND;
var differenceInMinutes:Number = differenceInSeconds / SECOND_PER_MINUTES;
var differenceInHouse:Number = differenceInMinutes / MINUTES_PER_HOUR;
var differenceInDays:Number = differenceInHouse / HOURS_PER_DAY;

if (date_now > date_end)
{                    
trace("expired");
}
else
{
trace("approved");                    
}

 

kglad
Community Expert
Community Expert
July 10, 2023

don't you see an error?

 

the third line of your code contains date_end which is undefined.

 

p.s. there's no need to convert from ms to anything else when doing date comparisons unless you want to check for a certain number of seconds, or minutes, or hours etc.