String was not recognised as a valid DateTime when parsing












0















So I have some code that will write the current DateTime to a txt file and then post some embeds, if the txt file doesn't exist. If it does, then it will read the file, check if the date in the file was over 1 minute ago, if it was it should send the same embed as the one that creates the file, if it hasn't been 1 minute then it will send an embed telling you to wait.



For some reason, the creating and sending aspect works fine, but if I run the command again, when it tries to convert the string to a DateTime it fails, saying "String was not recognized as a valid DateTime.". I have checked multiple websites and the datetime format I am using is correct. Any ideas?



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);









share|improve this question




















  • 1





    What does the DateTime string you are having difficulty parsing actually look like?

    – Jonathon Chase
    Nov 15 '18 at 21:25











  • When you print out the line to the console what does the string look like the second time? I'm guessing there is an extra character there

    – tmwoods
    Nov 15 '18 at 21:25
















0















So I have some code that will write the current DateTime to a txt file and then post some embeds, if the txt file doesn't exist. If it does, then it will read the file, check if the date in the file was over 1 minute ago, if it was it should send the same embed as the one that creates the file, if it hasn't been 1 minute then it will send an embed telling you to wait.



For some reason, the creating and sending aspect works fine, but if I run the command again, when it tries to convert the string to a DateTime it fails, saying "String was not recognized as a valid DateTime.". I have checked multiple websites and the datetime format I am using is correct. Any ideas?



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);









share|improve this question




















  • 1





    What does the DateTime string you are having difficulty parsing actually look like?

    – Jonathon Chase
    Nov 15 '18 at 21:25











  • When you print out the line to the console what does the string look like the second time? I'm guessing there is an extra character there

    – tmwoods
    Nov 15 '18 at 21:25














0












0








0








So I have some code that will write the current DateTime to a txt file and then post some embeds, if the txt file doesn't exist. If it does, then it will read the file, check if the date in the file was over 1 minute ago, if it was it should send the same embed as the one that creates the file, if it hasn't been 1 minute then it will send an embed telling you to wait.



For some reason, the creating and sending aspect works fine, but if I run the command again, when it tries to convert the string to a DateTime it fails, saying "String was not recognized as a valid DateTime.". I have checked multiple websites and the datetime format I am using is correct. Any ideas?



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);









share|improve this question
















So I have some code that will write the current DateTime to a txt file and then post some embeds, if the txt file doesn't exist. If it does, then it will read the file, check if the date in the file was over 1 minute ago, if it was it should send the same embed as the one that creates the file, if it hasn't been 1 minute then it will send an embed telling you to wait.



For some reason, the creating and sending aspect works fine, but if I run the command again, when it tries to convert the string to a DateTime it fails, saying "String was not recognized as a valid DateTime.". I have checked multiple websites and the datetime format I am using is correct. Any ideas?



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);






c# discord.net






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 23:38









Ctrl S

611524




611524










asked Nov 15 '18 at 21:21









Tim BirtlesTim Birtles

115




115








  • 1





    What does the DateTime string you are having difficulty parsing actually look like?

    – Jonathon Chase
    Nov 15 '18 at 21:25











  • When you print out the line to the console what does the string look like the second time? I'm guessing there is an extra character there

    – tmwoods
    Nov 15 '18 at 21:25














  • 1





    What does the DateTime string you are having difficulty parsing actually look like?

    – Jonathon Chase
    Nov 15 '18 at 21:25











  • When you print out the line to the console what does the string look like the second time? I'm guessing there is an extra character there

    – tmwoods
    Nov 15 '18 at 21:25








1




1





What does the DateTime string you are having difficulty parsing actually look like?

– Jonathon Chase
Nov 15 '18 at 21:25





What does the DateTime string you are having difficulty parsing actually look like?

– Jonathon Chase
Nov 15 '18 at 21:25













When you print out the line to the console what does the string look like the second time? I'm guessing there is an extra character there

– tmwoods
Nov 15 '18 at 21:25





When you print out the line to the console what does the string look like the second time? I'm guessing there is an extra character there

– tmwoods
Nov 15 '18 at 21:25












1 Answer
1






active

oldest

votes


















1














Use DateTime.ParseExact() or DateTime.TryParseExact(), e.g.



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


With DateTime.Parse() it could be problem if the server default format is different from yours.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53328067%2fstring-was-not-recognised-as-a-valid-datetime-when-parsing%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Use DateTime.ParseExact() or DateTime.TryParseExact(), e.g.



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


    With DateTime.Parse() it could be problem if the server default format is different from yours.






    share|improve this answer




























      1














      Use DateTime.ParseExact() or DateTime.TryParseExact(), e.g.



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


      With DateTime.Parse() it could be problem if the server default format is different from yours.






      share|improve this answer


























        1












        1








        1







        Use DateTime.ParseExact() or DateTime.TryParseExact(), e.g.



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


        With DateTime.Parse() it could be problem if the server default format is different from yours.






        share|improve this answer













        Use DateTime.ParseExact() or DateTime.TryParseExact(), e.g.



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


        With DateTime.Parse() it could be problem if the server default format is different from yours.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 16 '18 at 0:19









        Siu Pang Tommy ChoiSiu Pang Tommy Choi

        16310




        16310
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53328067%2fstring-was-not-recognised-as-a-valid-datetime-when-parsing%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Florida Star v. B. J. F.

            Error while running script in elastic search , gateway timeout

            Adding quotations to stringified JSON object values