0

How do I convert string to date time? I am getting "2019-06-07T02" as string. I want to format string to date like below examples

Ex: 2019-06-07T02 -- Friday January 07,2019 2:00 AM

Ex: 2019-06-07T14 --- Friday January 07,2019 2:00 PM

kame
  • 20,848
  • 33
  • 104
  • 159
PavanKumar GVVS
  • 859
  • 14
  • 45
  • 4
    Show. Code. Please. At least show your research so far. – ProgrammingLlama Jun 06 '19 at 07:02
  • 4
    You have two problems. Problem 1 - how to `ParseExact` a `string` to a `datetime`. Problem 2 - how to `ToString` from a `datetime` to a `string`. – mjwills Jun 06 '19 at 07:03
  • Look https://stackoverflow.com/questions/919244/converting-a-string-to-datetime And – JarITZ Jun 06 '19 at 07:05
  • Why down vote to my question . Is something wrong, pls. let me know will improve. but i see already 2 people answered and also up votes for answers. – PavanKumar GVVS Jun 06 '19 at 07:28
  • 1
    @PavanKumarGVVS - The reason for the down-vote is that this kind of question is asked again and again on SO. When you asked your question you would have been prompted with many duplicates - and they should have been sufficient for you to answer your own question before you posted. SO attempts to eliminate all duplicates. – Enigmativity Jun 06 '19 at 07:41

2 Answers2

4
var input = "2019-06-07T14";
var datetime = DateTime.ParseExact(input, "yyyy-MM-dd'T'HH", CultureInfo.InvariantCulture);
var output = datetime.ToString("dddd MMMM dd, yyyy h':'mm tt");

This does exactly what you need. (Source Docs)

Edit: I know it might be too manual but if you want to configure it more, this way you can, if not, go with oleksa's answer.

Anton Kahwaji
  • 467
  • 4
  • 15
0
var str = "2019-06-07T02";
var dt = DateTime.ParseExact(str, "yyyy-MM-ddThh", CultureInfo.InvariantCulture);

var longstr = dt.ToLongDateString() + dt.ToLongTimeString();

please note that ToLongDateString and ToLongTimeString depends on windows user regional settings

the documentation

oleksa
  • 3,688
  • 1
  • 29
  • 54