0

我想将 datetime 从 datetimepicker 转换为 unix 时间戳,但可以选择从文本框中选择指定的小时和分钟,然后显示在其他文本框中。到目前为止,这是我为日期制作时间戳的代码:

public static long ToUnixTimestamp(DateTime target)
    {
        var date = new DateTime(1970, 1, 1, 0, 0, 0, target.Kind);
        var unixTimestamp = System.Convert.ToInt64((target - date).TotalSeconds);

        return unixTimestamp;
    }

    public static DateTime ToDateTime(DateTime target, long timestamp)
    {
        var dateTime = new DateTime(1970, 1, 1, 0, 0, 0, target.Kind);

        return dateTime.AddSeconds(timestamp);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        DateTime dt = this.dateTimePicker1.Value.Date;
        textBox1.Text = string.Format("{0}", ToUnixTimestamp(dt));

    }

先感谢您

4

0 回答 0