How to get a string[] array into a DateTime? - c#

I'm new to C#.
I have different file names. for example:
C:\Test\ABCD\Warranty_2018_02_12__13_25_13.743.xml
from this name I want to get the date, like 12.02.2018 13:25:13
So the files are never the same.
My code:
public string GetCreationDate(string fileResult)
{
int index = fileResult.IndexOf("_");
if (index != -1)
{
string date = fileResult.Substring(index + 1, 20);
char[] delimiter = new char[] { '_' };
string[] dateWithoutLines = date.Split(delimiter, StringSplitOptions.RemoveEmptyEntries);
Array.Reverse(dateWithoutLines, 0, 3);
//Here is the error
//Guess it's because of the 'ToString()'
DateTime dateTime = DateTime.ParseExact(dateWithoutLines.ToString(), "dd/MM/yyyy hh:mm:ss",
System.Globalization.CultureInfo.InvariantCulture);
return dateTime.ToString();
}
return null;
}
In the debugger I have now 6 strings in the dateWithoutLines with the right dates. like "12" "02" "2018" ...
But then it says that is it not a right DateTime Format there. Okay, but if I delete the
ToString()
It says it can't convert string[] to string. So what is wrong here?

No need to split the original string, reverse, combine and so on.
Let DateTime.TryParseExact do all the work for you:
Also, consider returning a nullable DateTime (DateTime?) instead of a string:
public DateTime? GetCreationDate(string fileResult)
{
int index = fileResult.IndexOf("_");
if (index <= 0)
return null;
// check for string length, you don't want the following call to fileResult.Substring to throw an exception
if (fileResult.Length < index+20)
return null;
string date = fileResult.Substring(index + 1, 20);
DateTime dt;
if (DateTime.TryParseExact(date, "yyyy_MM_dd__HH_mm_ss", CultureInfo.InvariantCulture, DateTimeStyles.None, out dt))
return dt;
return null;
}

dateWithoutLines is a string[]. The ToString implementation for string[] yields "System.String[]" which is not a valid date. Furthermore DateTime.Parse does not take a string[], but only a string.
If the date format in the file name is always the same, you can use string.Format
var formattedDate = string.Format("{2}.{1}.{0} {3}:{4}:{5}", dateWithoutLines);
The numbers in the curly braces refer objects in the passed array by index, i.e. that {2} will be replaced by 12 in your example, {1} by 02 and so on. Please note that I've used the indices from the original order, not the reversed array.
Since you're parsing the date to format it, this way, no parsing is needed, since it's already formatted.

object instance dateWithoutLines is an array of strings , not a single string, thus you cant use method ToString().
So I believe you want something in the sense of :
foreach (string date in dateWithoutLines)
{
DateTime dateTime = DateTime.ParseExact(date, "dd/MM/yyyy hh:mm:ss",
System.Globalization.CultureInfo.InvariantCulture);
}
Notice, that since you have an array of strings, date is a string object so there is no need to call ToString() method for each date string instance

Related

How to parse a timespan in order to add it to a datetime?

I've got a string in the following format: 05/06/2019|1330|60
The output I'm looking for is: 05/06/2019T14:30:00
I'm attempting to parse out the TimeSpan portion right now:
public static string getProcedureEndingDateTime (string input) {
//05/06/2019|1330|60
string myDate = input.Split ( '|' ) [0];
DateTime myDateTime = DateTime.Parse (myDate);
string myTime = input.Split('|')[1];
string hours = myTime.Substring(0,2);
string minutes = myTime.Substring(2,2);
TimeSpan myTimeSpan = TimeSpan.Parse($"{hours}:{minutes}");
myDateTime.Add(myTimeSpan);
return myDateTime.ToString();
}
But right now, getting the following output:
To get the above output I'm calling my function like so:
Console.WriteLine (getProcedureEndingDateTime("05/06/2019|1330|60"));
How do I parse the string "1330" into a TimeSpan?
No need to us a Timespan here, just call ParseExact instead with a proper format to do it in one line.
var myDateTime = DateTime.ParseExact("05/06/2019|1330|60", "dd/MM/yyyy|HHmm|60", CultureInfo.InvariantCulture);
Console.WriteLine(myDateTime.ToString());
//this gives 2019-06-05 1:30:00 PM, format depends on your PC's locale
I don't know what the 60 part is, you can adjust the format or substring it out beforehand.
The problem is because Add() returns a new DateTime instance, which means you're currently discarding it. Store it, and return that from your function instead, like so:
var adjusted = myDateTime.Add(myTimeSpan);
return adjusted.ToString();
Try using the numeric values as exactly that, numbers.
Also, the other issue with your code is the DateTime.Add() method doesn't add to that DateTime variable. Instead it returns a new variable, which you are ignoring.
Try this:
public static string getProcedureEndingDateTime (string input) {
string[] parts = input.Split('|');
string myDate = parts[0];
DateTime myDateTime = DateTime.Parse (myDate);
string myTime = parts[1];
if (!int.TryParse(myTime.Substring(0,2), out int hours))
hours = 0;
if (!int.TryParse(myTime.Substring(2,2), out int minutes))
minutes = 0;
TimeSpan myTimeSpan = new TimeSpan(hours, minutes, 0);
myDateTime += myTimeSpan;
return myDateTime.ToString();
}
Assuming the date shown is May 6th (and not June 5th), and also assuming the 60 represents a time zone offset expressed in minutes west of GMT, and also assuming you want the corresponding UTC value, then:
public static string getProcedureEndingDateTime (string input) {
// example input: "05/06/2019|1330|60"
// separate the offset from the rest of the string
string dateTimeString = input.Substring(0, 15);
string offsetString = input.Substring(16);
// parse the DateTime as given, and parse the offset separately, inverting the sign
DateTime dt = DateTime.ParseExact(dateTimeString, "MM/dd/yyyy|HHmm", CultureInfo.InvariantCulture);
TimeSpan offset = TimeSpan.FromMinutes(-int.Parse(offsetString));
// create a DateTimeOffset from these two components
DateTimeOffset dto = new DateTimeOffset(dt, offset);
// Convert to UTC and return a string in the desired format
DateTime utcDateTime = dto.UtcDateTime;
return utcDateTime.ToString("MM/dd/yyyy'T'HH:mm:ss", CultureInfo.InvariantCulture);
}
A few additional points:
Not only is the input format strange, but so is your desired output format. It is strange to see a T separating the date and time and also see the date in the 05/06/2019 format. T almost always means to use ISO 8601, which requires year-month-day ordering and hyphen separators. I'd suggest either dropping the T if you want a locale-specific format, or keep the T and use the standard format. Don't do both.
In ISO 8601, it's also a good idea to append a Z to UTC-based values. For DateTime values, the K specifier should be used for that. In other words, you probably want the last line above to be:
return utcDateTime.ToString("yyyy-MM-dd'T'HH:mm:ssK", CultureInfo.InvariantCulture);
// outputs: "2019-05-06T14:30:00Z"
You might want to not format a string here, but instead return the DateTime or DateTimeOffset value. It's usually better to create a string only at the time of display.
Don't forget that the DateTime struct is immutable. In your question you were ignoring the return value of the Add method.

C# Date in CSV file

I have to change a csv file with several dates in it. Every row starts with a date followed whith data.
11-nov-2015,data,data,data
10-nov-2015,data,data,data
9-nov-2015,data,data,data
With the following code I put the data in the right place (20141109 (yyyymmdd))
string[] values = lines1[i].Split(',');
if (values.Length >= 3)
{
string[] parts = values[0].Split('-');
if (parts.Length == 3)
{
values[0] = String.Format("{0}-{1}-{2}", parts[2], parts[1], parts[0]);
lines1[i] = String.Join(",", values);
}
}
But two problems remain:
1) The month has to change from nov to 11
2) In the file I download the day for example 9-nov-2014 has to change to 09. for 8-nov-2014 to 08. So an extra 0.
How can this be solved in C#
Instead of making your own datetime format parser, you should use the one already available for you. DateTime.TryParseExact is your tool to convert a string in a date when you know the exact format.
Converting back the date, in the string format that you like, is another task easily solved by the override of ToString() specific for a datetime
string[] values = lines1[i].Split(',');
if (values.Length >= 3)
{
DateTime dt;
if (DateTime.TryParseExact(values[0], "d-MMM-yyyy",
System.Globalization.CultureInfo.CurrentCulture,
System.Globalization.DateTimeStyles.None, out dt))
{
values[0] = dt.ToString("yyyyMMdd");
lines1[i] = String.Join(",", values);
}
}
I would parse the string into a date and then write it back using a custom date format. From this link we can write this code:
String pattern = "dd-MMM-yyyy";
DateTime dt;
if (DateTime.TryParseExact(values[0], pattern, CultureInfo.InvariantCulture,
DateTimeStyles.None,
out dt)) {
// dt is the parsed value
String sdt = dt.ToString("yyyyMMdd"); // <<--this is the string you want
} else {
// Invalid string, handle it as you see fit
}

How to convert String "MM-DDTHH:mm" to DateTime in c#?

in my code i can get 2 types of string that represents dateTime:
1."2013-09-05T15:55"
2."09-05T19:10"
How do i convert it to a valid DateTime?
i tried the following code but it throws an exception for the second format:
String departureDateStr = "09-05T19:10";
DateTime dt = Convert.ToDateTime(departureDateStr);
how do i convert the second type of string to a valid DateTime ?
do i need some kind of string manipulation?
thx,
Amir
DateTime.TryParseExact has an overload that allows you to pass multiple formats as an array. Each date string is then compared with the various formats within the array so you don't need to know ahead of time which format to look for.
string d1 = "2013-09-05T15:55";
string d2 = "09-05T19:10";
string[] formats = new string[] { "yyyy-MM-ddTHH:mm", "MM-ddTHH:mm" };
List<string> dates = new List<string>() { d1, d2 };
foreach (string date in dates)
{
DateTime dt;
if (DateTime.TryParseExact(date, formats, CultureInfo.InvariantCulture, DateTimeStyles.None, out dt))
{
//dt successfully parsed
}
}
TryParseExact also returns false instead of throwing an exception if none of the formats in the array matched the input.
Use DateTime.ParseExact method with custom datetime format string:
string departureDateStr = "09-05T19:10";
string departureDateStr2 = "2013-09-05T19:10";
var dt = DateTime.ParseExact(departureDateStr, "MM-ddTHH:mm", System.Globalization.CultureInfo.InvariantCulture);
var dt2 = DateTime.ParseExact(departureDateStr2, "yyyy-MM-ddTHH:mm", System.Globalization.CultureInfo.InvariantCulture);
or universal call for both formats:
var dt = DateTime.ParseExact(departureDateStr, new[] { "MM-ddTHH:mm", "yyyy-MM-ddTHH:mm" }, System.Globalization.CultureInfo.InvariantCulture);
You can use DatetIme.ParseExact() method for this. It converts the specified string representation of a date and time to its DateTime equivalent using the specified format and culture-specific format information. The format of the string representation must match the specified format exactly.
String departureDateStr = "09-05T19:10";
IFormatProvider provider = System.Globalization.CultureInfo.InvariantCulture;
string format = "MM-ddTHH:mm";
DateTime parsedDate = DateTime.ParseExact(departureDateStr, format, provider);
If you need this conversion a lot of times, then you can even make it an extension method as below:
public static class StringExtensions
{
public static DateTime ToDate(this string str)
{
IFormatProvider provider = System.Globalization.CultureInfo.InvariantCulture;
string format = "MM-ddTHH:mm";
return DateTime.ParseExact(str, format, provider);
}
}

DateTime.ParseExact is failing (string not valid datetime format)

I am trying to use DateTime.ParseExact on a timestamp that uses the format M/d/yyyy HH:mm:ss:fff and the compiler is telling me that this is not going to happen.
An example of my timestamp is:
3/26/2013 14:37:05:553
...and an example of my code is, where _CultureInfo is en-us.
DateTime someDateTime = DateTime.ParseExact("3/26/2013 14:37:05:553","M/d/yyyy HH:mm:ss:fff", _CultureInfo.DateTimeFormat);
See below image of what's going on... am I missing something?
New Edit
I've tried a couple more things with still no luck:
Changing :fff to .fff
Changing _CultureInfo.DateTimeFormat to System.Globalization.CultureInfo.InvariantCulture and changing the d to dd as suggested below
Below is something you can throw into a Console and run to see exactly how this is behaving on my end.
class Program
{
static void Main(string[] args)
{
CsvImporter importer = new CsvImporter();
DateTime readtime = importer.Parse(#"""3/26/2013 14:37:07:238,00:00:01.6850000,23.138,23.488,23.175""");
Console.WriteLine(readtime.ToString());
}
}
class CsvImporter
{
public Char[] _SeparatorChars = new Char[] { ',' };
public DateTime Parse(string text)
{
System.Globalization.CultureInfo _CultureInfo = System.Globalization.CultureInfo.CurrentCulture;
string txt = text.Replace('"', ' ');
string[] columns = txt.Split(_SeparatorChars);
return DateTime.ParseExact(columns[0], "M/dd/yyyy HH:mm:ss:fff", _CultureInfo.DateTimeFormat);
//return DateTime.ParseExact(columns[0], "M/dd/yyyy HH:mm:ss.fff", System.Globalization.CultureInfo.InvariantCulture);
}
}
Try this (Changed d to dd and CultureInfo.InvariantCulture )
DateTime someDateTime = DateTime.ParseExact("3/26/2013 14:37:05:553", "M/dd/yyyy HH:mm:ss:fff", CultureInfo.InvariantCulture);
The issue is with string txt = text.Replace('"', ' ');
This is turning column[0] into [space]3/26/2013 14:37:05:553 instead of 3/26/2013 14:37:05:553 like I'd expect.
Changing this line to string txt = text.Replace(#"""", ""); solves the problem.
Whenever you call ParseExact, make sure you're using an unambiguous format string. In .NET, the / character in a format string is the system date separator, not an actual slash. Same for the : character in times.
To parse a string with your structure, escape out the slashes and colons with backslashes, like:
DateTime.ParseExact(s, #"M\/d\/yyyy HH\:mm\:ss\:fff", null)
// or
DateTime.ParseExact(s, "M\\/d\\/yyyy HH\\:mm\\:ss\\:fff", null)
This will tell the parser that you specifically want the forward-slash and colon, regardless of your system preferences or current culture.

Formatting any string to string "yyyy/MM/dd" [duplicate]

This question already has answers here:
Date formatting yyyymmdd to yyyy-mm-dd
(9 answers)
Closed 5 years ago.
I have many strings like "20120117" and "20120321". I need to convert it in a new string with this format: "2012/01/17" and "2012/03/21". So, there is a way to do this?
I try:
string dateString = string.format("{0:d", "20120321");
and
string dateString = string.format("{0:yyyy/MM/dd", "20120321");
and
string dateString = int.Parse("20120321").ToString("yyyy/MM/dd");
I all cases i don't reach my goal. =/
So, i can i do this?
OBS: There is a way to do that without parse to datetime?
You have to parse those values in DateTime objects first.
Example :
DateTime dt = DateTime.ParseExact("20120321", "yyyyMMdd", System.Globalization.CultureInfo.InvariantCulture);
var result = dt.ToString("yyyy/MM/dd");
Edit after your comments on other answers:
if you don't like parsing because it may throw excepations, you can always use TryParse, like this:
DateTime dt;
bool success = DateTime.TryParseExact("20120321", "yyyyMMdd", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out dt);
if (success)
{
var result = dt.ToString("yyyy/MM/dd");
}
Edit 2: Using TryParseExact with multiple formats:
DateTime dt;
string[] formats = { "yyyyMMdd", "yyyy" };
bool success = DateTime.TryParseExact("20120321", formats, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out dt);
if (success)
{
var result = dt.ToString("yyyy/MM/dd");
Console.WriteLine(result);
}
It will produce 2012/03/21 when using "20120321" as input value, and 2012/01/01 when using 2012 as input value.
DateTime.ParseExact("20120321", "yyyyMMdd", CultureInfo.CurrentCulture).ToString("yyyy/MM/dd")
You could parse the string but this method gives you validation without any extra code. Imagine receiving "20120230", "20110229", or any other invalid date.
From your comments:
There is a way to do that without parse to datetime?
Yes, absolutely. If you really want to propagate bad data through your system rather than highlighting that it's incorrect, you could definitely use:
// Make sure we'll always be able to get a result whatever the input.
string paddedInput = input + "????????";
string mightBeBadWhoKnows = string.Format("{0}/{1}/{2}",
paddedInput.Substring(0, 4), // The year, if we're lucky
paddedInput.Substring(4, 2), // The month, if we're lucky
paddedInput.Substring(6, 2)); // The day, if we're lucky
But why wouldn't you want to spot the bad data?
You absolutely should parse the data. If you want to be able to continue after receiving bad data having taken appropriate action, use DateTime.TryParseExact. If you're happy for an exception to be thrown, use DateTime.ParseExact. I'd suggest using the invariant culture for both parsing and formatting, unless you really want a culture-sensitive output.
Use DateTime.ParseExact to convert to a DateTime, then use ToString on that instance to format as you desire.
DateTime.ParseExact(dateString, "yyyyMMdd").ToString("yyyy/MM/dd");
EDIT: using Insert:
EDIT2: Fixed bugs :-)
var newString = dateString.Insert(4, "/").Insert(7, "/");
Just use string operations to insert the slashes:
string input = "20120321";
string dateString =
input.Substring(0, 4) + "/" +
input.Substring(4, 2) + "/" +
input.Substring(6);
or
string dateString = input.Insert(6, "/").Insert(4, "/");
If it's a date, try this:
DateTime.ParseExact("20120321","yyyyMMdd", null).ToString("yyyy/MM/dd", System.Globalization.DateTimeFormatInfo.InvariantInfo)
try this;
string dateString = DateTime.ParseExact("20120321", "yyyyMMdd",
null).ToShortDateString();
If your data is always in the same format and if you don't need to validate it, you can use the following snippet to avoid parsing it with DateTime
var strWithInsert = input.Insert(4,"/").Insert(7,"/");

Categories

Resources