Datetime does not update on Console.Writeline

Asked

Viewed 47 times

-2

When I click the button, something is written in the Console along with the date, but the time does not update.

Code:

private void button2_Click(object sender, EventArgs e)
    {
            Console.WriteLine("{0}", data.ToString("HH:mm:ss:fff"));
    }

Console: 11:37:55:540 11:37:55:540 11:37:55:540 11:37:55:540

  • 2

    And it won’t even update, you’ll have to build a whole algorithm that does this, it’s not something simple and it’s far from desired. First thing I need to know right now, you know, computers aren’t magic, you put something in, and he does what he wants. You have to build what you need. Anyway it’s already something very strange to mix Winforms with console.

  • I thought I was gonna update every "millisecond" because, like, he’s not sending five messages at the same time to have the same datetime. And I needed a basis for that algorithm that you mentioned, because I have no idea how to start

  • 1

    The problem is this, you want to make the ceiling, the finish, without laying the foundation, lay the brick.

  • Diego, why did you edit the question? Now the answers have become meaningless.

  • I reversed the issue because it invalidates the answers given. If you have another question, open a separate question. Recommended reading for better use of the site: [Tour], [Ask] and [Help].

  • Console with winforms? What is this lambança?

Show 1 more comment

3 answers

2

Datetime is not a "clock" that changes its representation over time.

Datetime is immutable, it represents the instant it was created.

To do what you want you must create a Datetime object whenever you want to display the current instant:

private void button2_Click(object sender, EventArgs e)
{
    Console.WriteLine("{0}", DateTime.Now.ToString("HH:mm:ss:fff"));
}
  • By the question and comments does not seem that way, he wants you to update the schedule automatically by yourself, which makes the question too wide.

  • Looks like it was, look at the answer his. Although what you’re thinking is also a possibility

  • I tried yours but did not update, only got with my reply, but thanks for trying to help.

  • So it’s all inconsistent, misconception, misconceptions, his response doesn’t make any sense, actually, I think he just wants to contradict me. It can be seen now how nothing makes sense. to tell the truth the right thing is to close as not clear of so much confusion that has in this question. If I did with interpolation that I like best he’d say it didn’t work either.

  • @Maniero Yeah, there’s no reason for him to say my code doesn’t work.

  • @ramaral yes, it works, not as described, but its works. I think even what his made more sense because he does not want the Now(At least it seems, as much in the question, answer and comments of it does not make sense, will know), he wants a date of the class. And she wants her to keep updating. Never mind, these cases are so confusing that it’s not worth wasting time. I would close the question.

  • @Maniero, I interpreted it as he wanted the instant when he clicked the button(Console.WriteLine() in the event of the button), however its interpretation is also valid. I even voted to close, but after his reply I do not know if I should.

  • His reply made me want to close ;) He was silent that nothing here makes sense. His answer did not solve anything, he created a variable with a value, changed its value then and then ignored it, and continued the code as the question, IE, there is no way this has solved anything. I went to see the previous questions and it’s just silly.

  • 1

    @ramaral https://answall.com/q/239676/101 the solution was already given. See the pattern of questions: https://answall.com/q/239738/101, https://answall.com/q/239650/101 and https://answall.com/q/239366/101. I won’t go into details, but you’re smart I think you’ll understand what’s going on here.

Show 4 more comments

0

I didn’t quite understand your question. But for what I intended you need to update the time each time you click the button. So, there’s the code

private void button2_Click(object sender, EventArgs e)
{
     var date = DateTime.Now;//Se esta variável já estiver criada, remova o 'var'
     Console.WriteLine("{0}", data.ToString("HH:mm:ss:fff"));
}

And if it’s to update the schedule alone, use a timer already using Winforms as well.

  • By the question and comments does not seem that way, he wants you to update the schedule automatically by yourself, which makes the question too wide.

  • @Cool It may be, but it makes no sense to update the time automatically since it is only written when the button is clicked

  • But that’s what he said in the comments. The click would probably just start this. Actually, I’m not even going to mess with that anymore, the question is very confusing, he doesn’t know what he wants, what works. It is only the comments for those who arrive here and see that none of this makes sense. From the beginning I saw that the Nowwas not the solution.

0

Now it’s updating..

private void button2_Click(object sender, EventArgs e)
        {
            DateTime dt = new DateTime();
            dt = DateTime.Now;
            Console.WriteLine("[{0}]", dt.ToString("HH:mm:ss:fff"));
            }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.