0
string date = DateTime.Now.ToString("M/d/yyyy h:m:ss tt", CultureInfo.InvariantCulture);
this.Lastvisit =  Convert.ToDateTime(DateTime.Now.ToString("MM/dd/yyyy h:m:ss"));

i have to pass the datetime like (MM/dd/yyyy) format.

Yeldar Kurmangaliyev
  • 33,467
  • 12
  • 59
  • 101
Jennifer
  • 23
  • 7
  • 2
    What do you mean by "I have to pass"? – Yeldar Kurmangaliyev Sep 06 '18 at 10:23
  • 6
    A `DateTime` value doesn't have a format. It's just the date and time. If you can give us more context, hopefully we can help you resolve the problem you face. – Jon Skeet Sep 06 '18 at 10:24
  • 1
    `DateTime` has no format. It's a binary value just like int and double. – Panagiotis Kanavos Sep 06 '18 at 10:24
  • Yep. A `DateTime` is the count of the number of ticks (100ns intervals) since midnight at the start of 01/01/0001. It *has* no format. – Damien_The_Unbeliever Sep 06 '18 at 10:24
  • I want to get date format has - mm/dd/yyyy h:m:ss – Jennifer Sep 06 '18 at 10:25
  • 1
    @Jennifer - then you want a *string*, not a `DateTime`. – Damien_The_Unbeliever Sep 06 '18 at 10:25
  • what you do here : `Convert.ToDateTime(DateTime.Now.ToString("MM/dd/yyyy h:m:ss"));` can be compared to `int x = Convert.ToInt32(123.ToString())` so you create a string representation from a valid `DateTime` object just to convert it back to a `DateTime` – Mong Zhu Sep 06 '18 at 10:26
  • @Jennifer read the comments again. Dates have no formats. If you want to display a date, explain what you actually want to do. *All* stacks and frameworks, (Winforms, webforms, MVC, Core) allow you to specify a format string in a data binding statement, a Format property or you can just use `String.Format()` passing the format you want – Panagiotis Kanavos Sep 06 '18 at 10:27
  • 1
    please answer the question in the first comment by @YeldarKurmangaliyev it is of utmost importance, otherwise we cannot really answer your question and have to close it as unclear – Mong Zhu Sep 06 '18 at 10:30
  • And equally, ADO.Net knows how to translate `DateTime` parameters into SQL `datetime` (and related) types, where again those types have no format either. You don't need strings there. – Damien_The_Unbeliever Sep 06 '18 at 10:31
  • @Jenifier: maybe you are confusing passing a datetime as a DateTime type parameter (which has no format) with displaying a datetime on screen (or in your debugger) which will always be a string that is formatted in some way? – mortb Sep 06 '18 at 10:32
  • @YeldarKurmangaliyev im having a datetime lastvisit , now i need to get current datetime of system and need to set date format as mm/dd/yyyy – Jennifer Sep 06 '18 at 10:39
  • You apparently don't believe what people are telling you. Perhaps you'll believe the [Reference Source](https://referencesource.microsoft.com/#mscorlib/system/datetime.cs,130)? That `dateData` field is the only instance field of the structure. – Damien_The_Unbeliever Sep 06 '18 at 10:41
  • You have a `DateTime`, then you convert it to a `String`, afterwards you convert it to a `DateTime`. You notice something? Hint: a DateTime has no format but just a value, a DateTime-string has. If `Lastvisit` is a `DateTime` property you should not care about formats. – Tim Schmelter Sep 06 '18 at 10:54
  • @TimSchmelter You're right . But if my system date format in yyyy-mm-dd i need to change format has mm/dd/yyyy so that i converted to string. – Jennifer Sep 06 '18 at 11:07
  • @Jennifer: the point is: you don't need to convert it to string at all so it has nothing to do with formats. `DateTime.Now` is already a `DateTime`, no need to fiddle around with strings. – Tim Schmelter Sep 06 '18 at 11:08
  • @TimSchmelter Thank you so much. Actually I need to push datetime in this format (mm/dd/yyyy) into SQLITE DB. Datetime gives whatever format in system but i need to change to specific format : Example-- if system date format is in yyyy-mm-dd then i want to change into this format mm/dd/yyyy – Jennifer Sep 06 '18 at 11:12
  • 3
    @Jennifer: No, no, no. You don't pass parameters as strings, you are vulnerable to sql-injection and you have issues like this and more if you do that. Use [parameterized queries](https://stackoverflow.com/a/42512840/284240) and pass the correct type `DateTime`. If your database stores this datetime as `varchar` you should change that immediately – Tim Schmelter Sep 06 '18 at 11:15
  • See https://stackoverflow.com/a/3078773/982149, while SQlite will store DateTime either as TEXT, REAL or INTEGER columns, you still can use DateTime from C#. – Fildor Sep 06 '18 at 11:43

1 Answers1

1

This is what you want to do:

string dateTime = DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss");
Marco Salerno
  • 5,131
  • 2
  • 12
  • 32