我希望从列表中获取最长日期<>。
但我不知道如何将x.time
转换为DateTime。
它总是在异常中显示如下错误:
String was not recognized as a valid DateTime
。
这是我的代码:
List<DTOSaveFromFile> lst = Load();
public static List<DTOSaveFromFile> Load()
{
string[] data = File.ReadAllLines(dataPath);
return (from i in data select new DTOSaveFromFile(i)).ToList<DTOSaveFromFile>();
}
foreach (var rows in lst)
{
DateTime biggest = lst
Select(x => DateTime.ParseExact(x.time, "d/M/yyyy", System.Globalization.CultureInfo.InvariantCulture)) //to get the list of `string` of `time` from your List<DTOSaveFromFile> + //To convert it to DateTime
.Max(); //Get the max among the DateTime
}
来自班级的 x.time
:
public class DTOSaveFromFile
{
public string reportName { get; set; }
public string eMail { get; set; }
public string time { get; set; }
}
如何解决此问题?感谢。
答案 0 :(得分:2)
添加DateTime.ParseExact
的格式以处理05-01-2016
案例
string[] formats = new string[] {"d/M/yyyy", "d-M-yyyy"}; //notice the dash
然后你的查询就像:
string[] formats = new string[] {"d/M/yyyy", "d-M-yyyy"}; //notice the dash
DateTime biggest = lst
.Select(x => DateTime.ParseExact(x.time, formats, //now use formats here
System.Globalization.CultureInfo.InvariantCulture,
System.Globalization.DateTimeStyles.AssumeLocal)) //to get the list of `string` of `time` from your List<DTOSaveFromFile> + //To convert it to DateTime
.Max(); //Get the max among the DateTime
请注意,DateTime.ParseExact
中的所有参数都可能需要System.Globalization.DateTimeStyles.AssumeLocal
,format
。
此外,每当您因新format
而发现错误时,只需在formats
数组中添加<?php
header('Content-type: application/json');
$status = array(
'type'=>'success',
'message'=>'Thank you for contact us. As early as possible we will contact you '
);
$name = @trim(stripslashes($_POST['name']));
$email = @trim(stripslashes($_POST['email']));
$subject = @trim(stripslashes($_POST['subject']));
$message = @trim(stripslashes($_POST['message']));
$email_from = $email;
$email_to = 'reflexivezhcet@gmail.com';//replace with your email
$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message;
$success = @mail($email_to, $subject, $body, 'From: <'.$email_from.'>');
echo json_encode($status);
die;