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

  • 相关阅读:
    django模板语言之Template
    python基础知识回顾之字符串
    在命令行中创建Django项目
    Ubuntu如何开启SSH服务
    跨过Django的坑
    python基础知识回顾之元组
    python基础知识回顾之列表
    SQL Server登录方式
    k8s或者docker容器在安装服务之后,用systemctl启动会报错:connection: Operation not permitted
    记一次oracle视图查询失效的情况,ERROR at line 1: ORA-04045: errors during recompilation/revalidation of NC633.BB_API_BUDGET_EXEINFO ORA-16000: database open for read-only access
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/3239039.html
Copyright © 2011-2022 走看看