A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: need a special kind of time object

  1. #1
    Member
    Join Date
    Oct 2002
    Location
    Tucson
    Posts
    97

    need a special kind of time object

    Ok here's the challenge.

    I need for people to type -30 into a field, then when they press TAB, have the field display a time that is 30 minutes prior to the current time.

    Is this possible?

    I tried to make it work with 2 text fields using this:


    on(keyPress "<Tab>"){
    if (tk != -30){
    feedback.text = "wrong";
    }else{
    mydate = new Date();
    code = mydate.getHours() + ":" + (mydate.getMinutes(-tk))
    if (mydate.getMinutes() > 59){
    code = mydate.getHours() - 1 + ":" + (mydate.getMinutes()-tk)
    }
    }
    }

    I failed badly. I'm frustrated because my code won't work, and that caused me to think I'm fat and ugly.

    Please help me get this time thing working so my self-esteem can be restored.

    Thanks,

    -Lemorris

  2. #2
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    This isn't the whole thing, but gives you the 'time manipulation' stuff. Dat2 will be a date object with the desired time.

    Date.getTime() is very useful because it represents the date as a single number (milliseconds since 1970). Then you can subract (or add) whatever you need, and then convert this back to a date object.

    For your date, I'm subtracting 30 minutes, expressed in milliseconds.


    code:

    dat = new Date();

    dat2 = new Date(dat.getTime() - 30*60*1000);

    trace("current time is : " + dat);
    trace("previous time is : " + dat2);


  3. #3
    Member
    Join Date
    Oct 2002
    Location
    Tucson
    Posts
    97
    Thanks Jbum, but I'm still floundering over here.

    I just need the hours and the minutes.

    Here's what I have:

    On frame one:


    stop()
    mydate = new Date();
    hrs = mydate.getHours()
    mins = mydate.getMinutes()
    if (mins < 10){
    mins = "0"+mins
    }
    current = hrs + ":" + mins



    On the stage I have an input text box named : tke

    I have a dynamic text box var name : current

    Another dynamic text box var name : feedback

    I have a button that will be invisible with this code:


    on(keyPress "<Tab>"){
    right = "-30"
    if (tke.text = right){
    if (mins < 30){
    mydate = new Date();
    mins2 = (mydate.getMinutes()+30)
    hrs2 = (mydate.getHours()-1)
    current = hrs2 + ":" + mins2
    }
    if(mins > 30){
    mydate = new Date();
    mins3 = (mydate.getMinutes()-30)
    hrs3 = mydate.getHours()
    current = hrs3 + ":" + mins3
    }

    if(tke.text != right){
    feedback.text = "wrong";
    }
    }
    }


    I need the user to type -30 into the tke input field and when they push tab the current time displayed is 30 minutes ago. I need to account for the hour if it's like 3:07 or something too.

    If they type anything else in the input text field I want an error message to pop up that say's wrong. But no other changes.

    I realize I may be over doing this, but I can't see another way.

    I was close with your method but I panicked again.

    Thanks for your help.

    -Lemorris

  4. #4
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    Here's a modification of my above script. It extracts the hours and minutes from dat2, and then adds leading zeros.

    code:

    dat = new Date();
    dat2 = new Date(dat.getTime() - 30*60*1000);
    hrs = dat2.getHours(); // extract hours
    mins = dat2.getMinutes(); // extract minutes
    hrs = String(hrs+100).substr(1,2); // add leading zeros
    mins = String(mins+100).substr(1,2); // add leading zeros
    prevtime = hrs + ':' + mins;
    trace("current time is : " + dat);
    trace("previous time (HH:MM) is : " + prevtime);


  5. #5
    Member
    Join Date
    Oct 2002
    Location
    Tucson
    Posts
    97
    Thanks Jbum!!

    Not only did I get the code to do what I needed. A hot chick winked at me while I was driving my '58 http://www.58vw.com. I owe you many thanks. My self esteem is restored.

    Here's how I used your code:

    onClipEvent(enterFrame){

    dat = new Date();
    dat1 = new Date(dat.getTime() - 30*60*1000);
    dat2 = new Date(dat.getTime() - 30*60*1000);
    hrs = dat2.getHours(); // extract hours
    hrs1 = dat.getHours(); // extract hours
    mins = dat2.getMinutes(); // extract minutes
    curmins = dat.getMinutes(); // extract minutes
    hrs = String(hrs+100).substr(1,2); // add leading zeros
    hrs1 = String(hrs1+100).substr(1,2); // add leading zeros
    mins = String(mins+100).substr(1,2); // add leading zeros
    curmins = String(curmins+100).substr(1,2); // add leading zeros
    prevtime = hrs + ':' + mins;
    curtime = hrs1 + ':' + curmins;
    //trace("current time is : " + dat);
    //trace("previous time (HH:MM) is : " + prevtime);
    }

    I doubled up so I could display both in text fields on the stage.

    Thanks again.

    -Lemorris

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center