zoukankan      html  css  js  c++  java
  • DELPHI下读取与设置系统时钟

    在DELPHI下读取与设置系统时钟
    很多朋友都想在自己的程序中显示系统时间
    这在DELPHI中十分容易
    利用DateToStr(Date)及TimeToStr(Time)函数即可实现。


    二者的函数原型如下:
    function DateToStr(Date:TDateTime):string;

    function TimeToStr(Time:TDateTime):string;

    其返回值均为String型。

    在程序中我们可以这样使用:

    Label1.Caption:=DateToStr(Date);

    Lable2.Caption:=TimeToStr(Time);

    二者分别调用了Delphi函数Date和Time读取系统日期和时间来实现的

    但只能读系统时钟

    而不能设置系统时钟。那么如何处理这一问题呢?这正是本文所要讨论的问题。

    既然Delphi没有提供如此功能

    但Delphi提供了调用WindowsAPI的接口。所以我们可以调用WindowsAPI函数来实现这一功能。具体方法如下:

    procedure TForm1.Button1Click(Sender:TObject);

    begin

    Edit1.Text:='97/10/30 10:09:59'; //注意:控制面板内时间格式要为YY/MM/DD

    end;

    procedure TForm1.Button2Click(Sender:TObject);

    var  systemtime:Tsystemtime;

    DateTime:TDateTime;

    begin

    DateTime:=StrToDateTime(Edit1.text); //获得时间(TDateTime格式)

    DateTimeToSystemTime(DateTime

    systemtime); //把Delphi的TDateTime格式转化为API的TSystemTime格式

    SetLocalTime(SystemTime); //设置系统时间

    GetLocalTime(SystemTime); //读取系统时间

    DateTime:=SystemTimeToDateTime(SystemTime); //把API的TSystemTime格式 转化为 Delphi的TDateTime格式

    Edit2.Text:=DateTimeToStr(DateTime); //显示当前系统的时间

    end;

    另外

    还有好多其它的Delphi函数和API函数供我们使用

    如: StrToDate、StrToTime、DateTimeToStr、StrToDateTime、DateTimeToSystemTime、SystemTimeToDateTime、DateTimeToTimeStamp、TimeStampToDateTimeCompareFileTime、DosDateTimeToFileTime、FileTimeToDosDateTime、FileTimeToLocalFileTime、FileTimeToSystemTime、GetFileTime、SetFileTime、GetSystemTime(格林威治时间)、SetSystemTime.GetSystemTimeAdjustment

    SetSystemTimdAdjustment。


    //TSystemTime的格式

    PSystemTime = ^TSystemTime;

    TSystemTime = record

    wYear: Word;

    wMonth: Word;

    wDayOfWeek: Word; //当前的系统时间是星期几

    wDay: Word;

    wHour: Word;

    wMinute: Word;

    wSecond: Word;

    wMilliseconds: Word;

    end;

    //TDateTime的格式

    TDateTime = type Double

  • 相关阅读:
    20Spring_JdbcTemplatem模板工具类
    19Spring_AOP编程(AspectJ)_使用@Pointcut注解来定义切点
    18Spring_AOP编程(AspectJ)_AspectJ的各种通知总结
    17Spring_AOP编程(AspectJ)_AspectJ的注解编程
    14Spring_AOP编程(AspectJ)_环绕通知
    android ViewPager滑动事件讲解
    为listview的item中的元素设置onclick事件
    Android EditText光标颜色 与inputType
    【Android】Android内存溢出问题---用自行开辟的空间进行对内存管理
    【Android】eclipse打不开的解决办法和“Jar mismatch! Fix your dependencies”的解决
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/3239039.html
Copyright © 2011-2022 走看看