Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

how do i change the date or time?

mydelphimydelphi Member Posts: 62
Please put me out of my misery. I don't get it, how do I change the date on my computer
like I can in dos?

I only need to change the date, not the time. Every time I change the date, the time is
changed. But the code seems to require the time fields to be entered in order to work and
that seem a bit unfair. There must be a better way!

In dos, at the c: you change eitehr one, date or time:

date: 1/1/2000 --- a new year
time: 8:30am ----- time to go to work!

Anyway, after google searching around I found some code snips that showed an example.
I couldn't belive how much code it requires just to change one thing!! But, here is the
code, though I did a lot of changing it around and reworking it for my own tests. But this
routine changes my time to 5:00pm every time, even though I only want to change the date!

If someone can show me an easy one-liner, I would be very greatful. I will be using the code
for both my windows xp and older windows 98se computers.

Thanks,
mydelphi

[code]procedure TForm1.Button1Click(Sender: TObject);
var
ADate, ATime : TDateTime;
begin
// convert string to date..
adate := strTodate(eb3.text);
atime := strToTime(eb4.text);

FillChar (MyTime, sizeof(MyTime), #1);
DateTimeToSystemTime(adate,mytime);

inc(mytime.wDay);
setSystemTime(mytime);

// if not SetSystemTime (MyTime) then ShowMessage ('Failure!');
end;[/code]

Comments

  • mydelphimydelphi Member Posts: 62
    It is truly an impossible task, and can not be done!

    I admit that this is got to be the single most difficult task to accomplish.
    And I still haven't figured it out. I now belive it is NOT possible to change
    the DATE on one's computer through delphi code, at least not with borland
    delphi 6, but maybe a higher version they may have changed some things
    and it might be possible. After searching around the internet and google
    for the last week now, and trying many different code scenarios, I have
    come to a point of dis-belief and madness! It can't be done!

    The closest I came was, if I change the date, the time is reset to 5pm/am
    If I change the time, the date is reset to 1899. Or, if I change the date,
    the time and date gets bumped back a day, (though that code was removed
    from the previous post and resolved.)

    There is no SetDate() function. Nor is there a SetTime() function. Nor is
    there a SetDateTime() function, etc.

    I can not believ that you have to juggle so many things around just to change
    the date or the time or both and in any event they both get screwed up.

    Now, I'm not trying to convert a time to string or string to time. I am trying
    to RESET either a date or a time ON MY COMPUTER, NOT A VARIABLE!

    I'm sorry, but I'm very aggrevated. This is IMPOSSIBLE!!

    I close with final words. If I ever do find the answer, I will post it so that
    no else EVER goes through what I have (and still am) to this day.

    Thank you,
    mydelphi

    [b]About the code snipplet:[/b]
    [i]The code is already way too much, for a simple task, all these variables and
    things to set up and all. Still, the only tricky part is this mytime. I've played
    around with various scenarios but it (or the code as a whole) screws the
    time or date or both up. But, because the setSystemTime() function requires
    it, I included it. But I have set it up properly in other code examples. But
    this is what I have now.[/i]

    [code]procedure TForm1.Button1Click(Sender: TObject);
    var
    ADate, ATime : TDateTime;
    mytime : TsystemTime
    begin
    // convert string to date..
    ADate := strTodate(eb3.text);
    ATime := strToTime(eb4.text);

    DateTimeToSystemTime(adate, mytime);

    setSystemTime(mytime);
    end;[/code]
  • zibadianzibadian Member Posts: 6,349
    : It is truly an impossible task, and can not be done!
    :
    : I admit that this is got to be the single most difficult task to
    : accomplish.
    : And I still haven't figured it out. I now belive it is NOT possible
    : to change
    : the DATE on one's computer through delphi code, at least not with
    : borland
    : delphi 6, but maybe a higher version they may have changed some
    : things
    : and it might be possible. After searching around the internet and
    : google
    : for the last week now, and trying many different code scenarios, I
    : have
    : come to a point of dis-belief and madness! It can't be done!
    :
    : The closest I came was, if I change the date, the time is reset to
    : 5pm/am
    : If I change the time, the date is reset to 1899. Or, if I change
    : the date,
    : the time and date gets bumped back a day, (though that code was
    : removed
    : from the previous post and resolved.)
    :
    : There is no SetDate() function. Nor is there a SetTime() function.
    : Nor is
    : there a SetDateTime() function, etc.
    :
    : I can not believ that you have to juggle so many things around just
    : to change
    : the date or the time or both and in any event they both get screwed
    : up.
    :
    : Now, I'm not trying to convert a time to string or string to time.
    : I am trying
    : to RESET either a date or a time ON MY COMPUTER, NOT A VARIABLE!
    :
    : I'm sorry, but I'm very aggrevated. This is IMPOSSIBLE!!
    :
    : I close with final words. If I ever do find the answer, I will post
    : it so that
    : no else EVER goes through what I have (and still am) to this day.
    :
    : Thank you,
    : mydelphi
    :
    : [b]About the code snipplet:[/b]
    : [i]The code is already way too much, for a simple task, all these
    : variables and
    : things to set up and all. Still, the only tricky part is this
    : mytime. I've played
    : around with various scenarios but it (or the code as a whole) screws
    : the
    : time or date or both up. But, because the setSystemTime() function
    : requires
    : it, I included it. But I have set it up properly in other code
    : examples. But
    : this is what I have now.[/i]
    :
    : [code]: procedure TForm1.Button1Click(Sender: TObject);
    : var
    : ADate, ATime : TDateTime;
    : mytime : TsystemTime
    : begin
    : // convert string to date..
    : ADate := strTodate(eb3.text);
    : ATime := strToTime(eb4.text);
    :
    : DateTimeToSystemTime(adate, mytime);
    :
    : setSystemTime(mytime);
    : end;[/code]:

    I've played with it also somewhat in D5. Here's what I've got:
    [code]
    var
    MySysDateTime: SYSTEMTIME;
    begin
    GetSystemTime(MySysDateTime);
    with MySysDateTime do
    begin
    wYear := 2001;
    wMonth := 4;
    wDay := 22;
    end;
    SetSystemTime(MySysDateTime);
    end;
    [/code]
    It works quite well, except for the regional settings. Thus the time might still change, if you're in a different time zone than UTC.
  • mydelphimydelphi Member Posts: 62
    : I've played with it also somewhat in D5. Here's what I've got:
    : [code]:
    : var
    : MySysDateTime: SYSTEMTIME;
    : begin
    : GetSystemTime(MySysDateTime);
    : with MySysDateTime do
    : begin
    : wYear := 2001;
    : wMonth := 4;
    : wDay := 22;
    : end;
    : SetSystemTime(MySysDateTime);
    : end;
    : [/code]:
    : It works quite well, except for the regional settings. Thus the time
    : might still change, if you're in a different time zone than UTC.

    Thank you for trying your hand at it. At least I know that you can't, niether!

    This is a nightmare. I mean, you can't set a date or a time or both, on your computer.
    With the exception of resorting to manaully inputting it as Zibadian's example. The
    problem with his is that you have to do it manually. You have to look up the date and
    then type it in as a string to wYear, wMonth, and wDay. That's not what I'm looking
    for. I know you can do this in MS Access quite simple because I do it all the time.

    So what I'm looking for is the simple and quick way.

    For example:

    DATE:=5/2/2008

    done!

    TIME:=10:58pm

    done!

    TimeDate.date := edit1.text, where string value is '5/2/2008'
    TimeDate.time := edit2.text, where string value is '10:58pm'

    Unfortunately, DateTimeToSystemTime(ADate, SystemTime); does not work properly.
    The date resets to 1899, and time, strangely enough is set to 5:00am, every time.
    Then, I have to re-roll the time back. And every time I test various test codes, I
    have to do this, every time. And after half dozen times doing this, I give up. A half
    hour has past and I got no where!

    All I can say, is the somewhere along the line, Borland screwed up with the setting
    of the date and time on one's computer.

    Anybody got any other ideas?

    Maybe a c/c++ port to a DLL file. Please, I'd be greatful if there is a c/c++ dll file
    that I can import and use the library function to change the freeken date and/or time!

    Thanks for everyone's patience.

    -mydelphi
  • zibadianzibadian Member Posts: 6,349
    : : I've played with it also somewhat in D5. Here's what I've got:
    : : [code]: :
    : : var
    : : MySysDateTime: SYSTEMTIME;
    : : begin
    : : GetSystemTime(MySysDateTime);
    : : with MySysDateTime do
    : : begin
    : : wYear := 2001;
    : : wMonth := 4;
    : : wDay := 22;
    : : end;
    : : SetSystemTime(MySysDateTime);
    : : end;
    : : [/code]: :
    : : It works quite well, except for the regional settings. Thus the time
    : : might still change, if you're in a different time zone than UTC.
    :
    : Thank you for trying your hand at it. At least I know that you
    : can't, niether!
    :
    : This is a nightmare. I mean, you can't set a date or a time or
    : both, on your computer.
    : With the exception of resorting to manaully inputting it as
    : Zibadian's example. The
    : problem with his is that you have to do it manually. You have to
    : look up the date and
    : then type it in as a string to wYear, wMonth, and wDay. That's not
    : what I'm looking
    : for. I know you can do this in MS Access quite simple because I do
    : it all the time.
    :
    : So what I'm looking for is the simple and quick way.
    :
    : For example:
    :
    : DATE:=5/2/2008
    :
    : done!
    :
    : TIME:=10:58pm
    :
    : done!
    :
    : TimeDate.date := edit1.text, where string value is '5/2/2008'
    : TimeDate.time := edit2.text, where string value is '10:58pm'
    :
    : Unfortunately, DateTimeToSystemTime(ADate, SystemTime); does not
    : work properly.
    : The date resets to 1899, and time, strangely enough is set to
    : 5:00am, every time.
    : Then, I have to re-roll the time back. And every time I test
    : various test codes, I
    : have to do this, every time. And after half dozen times doing this,
    : I give up. A half
    : hour has past and I got no where!
    :
    : All I can say, is the somewhere along the line, Borland screwed up
    : with the setting
    : of the date and time on one's computer.
    :
    : Anybody got any other ideas?
    :
    : Maybe a c/c++ port to a DLL file. Please, I'd be greatful if there
    : is a c/c++ dll file
    : that I can import and use the library function to change the freeken
    : date and/or time!
    :
    : Thanks for everyone's patience.
    :
    : -mydelphi

    I've kept the code as simple as possible, for this example. Obviously the values of the fields don't need to be hard coded, but can com from anywhere.
    [code]
    procedure TForm1.NMHttp1Success(Cmd: CmdType);
    var
    MySysDateTime: SYSTEMTIME;
    s: string;
    begin
    GetSystemTime(MySysDateTime);
    with MySysDateTime do
    begin
    wYear := StringSplit(NMHttp1.Body, 0, '/');
    wMonth := StringSplit(NMHttp1.Body, 1, '/');
    wDay := StringSplit(NMHttp1.Body, 2, '/');
    s := StringSplit(NMHttp1.Body, 1, ' ');
    wHour := StringSplit(NMHttp1.Body, 0, ':');
    wMinute := StringSplit(NMHttp1.Body, 1, ':');
    wSecond := StringSplit(NMHttp1.Body, 2, ':');
    end;
    SetSystemTime(MySysDateTime);
    end;
    [/code]
    This example requests the date from a website and parses the response into the date. This uses the TNMHTTP component and StringSplit() (in CodePedia). The response from the webserver is formatted as:
    [code]
    dd/MM/yy hh:mm:ss
    [/code]
    This takes care of everything, except the regional settings in Windows. Sure you could blame Borland for the mistakes of Microsoft, but SetSystemTime() is one of the may Windows API functions.

    As for the (mis)workings of DateTimeToSystemTime() that's quite simple:
    in your original code you never set the time! This means that the time is set to 0, which corresponds to 5:00am for some reason. Also beware that StrToDate() takes a specific format in the string. Any other thing might either raise an exception or produce incorrect results.
Sign In or Register to comment.