当布局为RightToLeft时,将字符串解析为DateTime类型

时间:2017-05-04 12:30:12

标签: c# parsing datetime

我想解析标签中显示的字符串值,然后将该值与其他值一起插入数据库中的表中。所以我有包含插入的存储过程语句和执行执行插入语句的方法,最后我在我的按钮单击事件中调用该方法来执行插入方法,但它给我一个异常消息“输入字符串格式不正确”和一个提示是“在将每个变量放入datetime对象之前解析字符串以获取日期”

在按钮点击事件中调用此方法是:

DateTime billDateTime = DateTime.Parse(pBillDateLbl.Text);
purchaseCashBill.InsertGeneralCashBillInfo(billDateTime, pBillUserNameTxt.Text, Convert.ToInt32(pBillCompCmbo.SelectedValue), pBillCompCmbo.SelectedText, Convert.ToDouble(pBillItmTtlPrice.Text), true);  

我也尝试使用这种方式削减字符串,但出现相同的异常:

DateTime billDateTime = DateTime.ParseExact(
   pBillDateLbl.Text, 
  "d/M/yyyy hh:mm:ss", 
   System.Globalization.CultureInfo.CurrentCulture);

并且标签的值采用以下形式:

pBillDateLbl.Text = DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss");  

在我的申请中显示如下:05:45:22 05/04/2017

1 个答案:

答案 0 :(得分:0)

根据您在评论中提供的值myFolder = '../Test'; if option1 == 1 ext = 'EW'; filePattern1 = fullfile(myFolder, '*.EW1'); % input motion datFiles1 = dir(filePattern1); filePattern2 = fullfile(myFolder, '*.EW2'); % surface motion datFiles2 = dir(filePattern2); elseif option1 == 2 ext = 'NS'; filePattern1 = fullfile(myFolder, '*.NS1'); % input motion datFiles1 = dir(filePattern1); filePattern2 = fullfile(myFolder, '*.NS2'); % surface motion datFiles2 = dir(filePattern2); end parfor k = 1:length(datFiles1) baseFileName1 = datFiles1(k).name; baseFileName2 = datFiles2(k).name; fullFileName1 = fullfile(myFolder, baseFileName1); fullFileName2 = fullfile(myFolder, baseFileName2); %display file importing sequence fprintf(1, 'Now processing %s\n', baseFileName1); record1 = load(baseFileName1); fprintf(1, 'Now processing %s\n', baseFileName2); record2 = load(baseFileName2); npts2 = length(record2(:,1)); %this is the end of file importing part and the following is to process the imported data, and as that is not relevant to this issue, I didn’t show them. end ,您可以清楚地看到格式字符串的顺序错误。此外,它最后还包含一个额外的空间。试试这个:

05:43:34 04/05/2017