0

我有一个 C# 类,它接受作业打印机及其一些属性,包括 TimeSubmitted,存储为 DateTime,格式如下:20130608204517.699000-300

我用这个属性定义了一个对象:

public class PrinterJob
{
    public String Document { get; set; }
    public string JobName { get; set; }
    public UInt32 JobId { get; set; }
    public string JobStatus { get; set; }
    //===========================================================================
    // This property doesn't cast adecuate 
    //===========================================================================
    public Datetime TimeSubmitted { get; set; }
}

当我尝试投射 DateTime 时(在运行时):

foreach (ManagementObject prntJob in prntJobCollection)
{
    System.String jobName = prntJob.Properties["Name"].Value.ToString();

    //Job name would be of the format [Printer name], [Job ID]
    char[] splitArr = new char[1];
    splitArr[0] = Convert.ToChar(",");
    string prnterName = jobName.Split(splitArr)[0];
    if (String.Compare(prnterName, printerName, true) == 0)
    {
        PrinterJob prtobj = new PrinterJob();
        prtobj.Document = prntJob.Properties["Document"].Value.ToString();
        prtobj.JobName = prntJob.Properties["Name"].Value.ToString();
        var id = prntJob.Properties["JobId"].Value;
        prtobj.JobId = (UInt32)prntJob.Properties["JobId"].Value;
        prtobj.JobStatus = prntJob.Properties["JobStatus"].Value.ToString();
        //============================================================================  
        // Here The cast doesn't work. It comes as: 20130608204517.699000-300 
        // It throws an InvalidCastException
        //============================================================================  
        prtobj.TimeSubmitted = (DateTime)prntJob.Properties["TimeSubmitted"].Value;
        jobList.Add(prtobj);
    }
} 

将这个值转换为 DateTime 的正确方法是什么?

4

2 回答 2

2

添加参考System.Management

var datetime = ManagementDateTimeConverter.ToDateTime("20130608204517.699000-300");
于 2013-06-09T18:27:10.760 回答
0

这不是一个简单的日期时间吗?(即yyyyMMddHHmmss)我不知道点后面的部分是什么意思。

于 2013-06-09T18:11:44.820 回答