解析时无法将字符串识别为有效的DateTime

蒂姆·比特尔斯

因此,我有一些代码将当前的DateTime写入txt文件,然后发布一些嵌入(如果txt文件不存在)。如果是的话,它将读取文件,检查文件中的日期是否在1分钟前,如果是,它应该发送与创建文件的日期相同的嵌入,如果还没有1分钟,那么它将发送一个嵌入消息,告诉您等待。

由于某种原因,创建和发送方面可以正常工作,但是如果我再次运行该命令,则当它尝试将字符串转换为DateTime时,它将失败,并说“字符串未被识别为有效的DateTime”。我已经检查了多个网站,并且我使用的日期时间格式正确。有任何想法吗?

string FilePath22 = Environment.CurrentDirectory + "/servers/" + Context.Guild.Id + ".txt";
string FilePath = Environment.CurrentDirectory + "/descriptions/" + Context.Guild.Id + ".txt";
string FilePath2 = Environment.CurrentDirectory + "/invites/" + Context.Guild.Id + ".txt";
var chnl = Context.Client.GetChannel(511281184760791056) as ITextChannel;
string invitelink = System.IO.File.ReadAllText(FilePath2);
string desclink = System.IO.File.ReadAllText(FilePath);
var builder2 = new EmbedBuilder()
        .WithColor(Color.Orange)
        .WithAuthor(Context.Guild.Name)
        .WithThumbnailUrl(Context.Guild.IconUrl)
        .WithUrl(invitelink)
        .AddField("Information", "**Owner:** " + Context.Guild.Owner + Environment.NewLine +
        "**Description:** " + Environment.NewLine + Environment.NewLine + desclink + Environment.NewLine + Environment.NewLine + "**Invite:** " + invitelink)
        .AddField("Other Info", "**Members:** " + Context.Guild.MemberCount + Environment.NewLine + "**Emotes:** " + Context.Guild.Emotes.Count + Environment.NewLine + "**Roles:** " + Context.Guild.Roles.Count + Environment.NewLine + "**Created At:** " + Context.Guild.CreatedAt.Date);
        var embed2 = builder2.Build();
        await chnl.SendMessageAsync(embed: embed2);
        var builder44 = new EmbedBuilder()
        .WithColor(Color.Orange)
        .WithTitle("Server Bumped!");
var embed44 = builder44.Build();

if (!File.Exists(FilePath22))
{
    await ReplyAsync(embed: embed44);
    System.IO.File.WriteAllText(FilePath22, DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss", CultureInfo.InvariantCulture));
    return;
}

string readit = System.IO.File.ReadAllText(FilePath22);
Console.WriteLine(readit);
var converted = DateTime.Parse(readit); //problem
Console.WriteLine(converted);
if (HoursPassed(converted))
{
    await ReplyAsync(embed: embed44);
    System.IO.File.WriteAllText(FilePath22, DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss", CultureInfo.InvariantCulture));
    return;
}

var builder10 = new EmbedBuilder()
        .WithColor(Color.Orange)
        .WithTitle("You must wait 24 hours before bumping your server! Try again later.");
var embed10 = builder10.Build();
await ReplyAsync(embed: embed10);
兆邦蔡志明

使用DateTime.ParseExact()DateTime.TryParseExact(),例如

DateTime converted;
DateTime.TryParseExact(readit, "MM/dd/yyyy HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.None, out converted);

有了DateTime.Parse()它可能是问题,如果服务器默认格式是从你的不同。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

无法将字符串识别为Datetime的有效解析字符串

Datetime.ParseExact“无法将字符串识别为有效的DateTime”错误

尝试使用linq筛选数据时,无法将字符串识别为有效的DateTime

无法将字符串识别为有效的DateTime“格式dd / MM / yyyy”

无法将字符串识别为有效的DateTime格式异常

无法将字符串识别为DateTime.ParseExact的有效参数

无法将字符串识别为有效的DateTime c#

无法将字符串识别为有效的DateTime

使用DateTime.ParseExact C#无法将字符串识别为有效的DateTime

无法将字符串识别为有效的DateTime。在Windows Server 2012上使用ParseExact

无法将字符串识别为有效的DateTime。2015年6月26日

在C#中无法将字符串识别为有效的日期时间

字符串为15/07/15时,字符串未被识别为有效的DateTime

将字符串转换为日期获取(未将字符串识别为有效的DateTime)

DateTime.ParseExact:“字符串未被识别为有效的DateTime”

System.FormatException:字符串未被识别为有效的DateTime-尝试转换MM / DD / YYYY时

具有“ 3”参数的powershell“ ParseExact”:“未将字符串识别为有效的DateTime

在ParseExact中,毫秒未将字符串识别为有效的DateTime

字符串未被识别为有效的DateTime c#

添加datetimepicker后,字符串未被识别为有效的datetime

DateTime.Parse() -- 字符串未被识别为有效的日期时间

HTML敏捷性和字符串未被识别为有效的DateTime

Dapper:字符串未被识别为有效的DateTime

字符串未被识别为有效的DateTime,因为星期几不正确

Xamarin.Forms 字符串未被识别为有效的 DateTime

ASP.NET“字符串未被识别为有效的DateTime。”

C#字符串未在Parameters.AddWithValue中识别为有效的DateTime

该字符串未被识别为有效的DateTime错误

字符串未被识别为有效的 DateTime,存储过程为空参数