我想将datetimepicker中的datetime转换为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));
}
提前谢谢