You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#如何将Process.StartTime的DateTime转换为指定格式?

DateTime格式转换解决方案:从"10/24/2022 4:01:35 PM"到"24/10/2022 16:01:35"

以下是主流编程语言的实现方案:

C# 实现

  • 解析原字符串为DateTime对象:
    string originalDateStr = "10/24/2022 4:01:35 PM";
    DateTime dateTime = DateTime.ParseExact(originalDateStr, "MM/dd/yyyy h:mm:ss tt", System.Globalization.CultureInfo.InvariantCulture);
    
  • 转换为目标格式字符串:
    string targetDateStr = dateTime.ToString("dd/MM/yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);
    // 输出结果:24/10/2022 16:01:35
    

Python 实现

from datetime import datetime

original_date_str = "10/24/2022 4:01:35 PM"
# 解析原格式
date_obj = datetime.strptime(original_date_str, "%m/%d/%Y %I:%M:%S %p")
# 生成目标格式字符串
target_date_str = date_obj.strftime("%d/%m/%Y %H:%M:%S")
print(target_date_str)  # 输出结果:24/10/2022 16:01:35

格式占位符说明

  • 原格式核心标识:
    • MM/%m:两位数字月份
    • dd/%d:两位数字日期
    • h/%I:12小时制小时数
    • tt/%p:AM/PM时段标识
  • 目标格式核心标识:
    • HH/%H:24小时制小时数

内容的提问来源于stack exchange,提问作者Rebecca

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 05:50:26