Trimend (Date and Time) - c#

2014-01-02 08:18:21""
I am reading this column from an excel file, but I need to remove the "" for it to be a valid date, this is my code below, please kindly advice.
n.RBank_Authorizer_date = DateTime.Parse(dRow[4].ToString()>TrimEnd);

You can use the overload of TrimEnd that takes a params char[]:
str = str.TrimEnd('"');
or with your code:
n.RBank_Authorizer_date = DateTime.Parse(dRow[4].ToString().TrimEnd('"'));
Note that dRow[4].ToString() can throw an exception if the field can be null. I would use the strongly typed DatRow.Field extension method that supports also nullable types.
n.RBank_Authorizer_date = DateTime.Parse(dRow.Field<string>(4).TrimEnd('"'));
By the way, if the DataRow already stores this datetime as DateTime you don't need to convert it to string and back to DateTime at all. You just have to use the correct type with Field.

You can always use the Replacemethod from string:
var dateString = dRow[4].ToString().Replace("\"", "");
The "ToString" should not be needed, but just in case you are getting something different than a string :)

As an alternative, if your all rows ends with "", you can specify this in your custom date and time format and can use DateTime.ParseExact method like;
string s = #"2014-01-02 08:18:21""";
var dt = DateTime.ParseExact(s, #"yyyy-MM-dd HH:mm:ss'""'",
CultureInfo.InvariantCulture);
Console.WriteLine(dt);

Related

Parsing a string like "03/2020" to a DateTime variable

I have to parse a string, which always look like 03/2020 (so the format is MM/yyyy), to a DateTime variable.
How do I achieve it the most proper way? (Without splitting the string into substrings)
I already found the DateTime.ParseExact function, but I'm confused by the third parameter (culture-specific format information).
Is the DateTime.ParseExact the way to go or is there a better function/way to achieve the goal?
var inputString = "03/2020";
var inputStringFormat = #"MM/yyyy";
// var inputStringAsDateTime = ???
Edit 1
As additional info I have to say, that the string (inputString) is read from a barcode. So the separator in it is always the /.
Tim Schmelter explained in his answer, how to mask such separators.
It looks like I have to change my inputStringFormat variable to:
var inputStringFormat = #"MM'/'yyyy";
If the first day of the month is good for you, you can use DateTime.ParseExact:
var d = DateTime.ParseExact(inputString, inputStringFormat, CultureInfo.InvariantCulture);
Usually, i use ToDateTime method of Convert class. I have tested it, it's working.
string inputString = "03/2020";
DateTime datetime = Convert.ToDateTime(inputString));
It will give output like 3/1/2020 12:00:00 AM means it will gives you output according to your system's date format.
With such simple date format you can use
var inputArr = inputString.split(#"/");
var inputStringAsDateTime = new DateTime(inputArr[0], inputArr[1], 1);
however for culture you can use constant
CultureInfo.InvariantCulture

Change the datetime format from '2014-03-11T14:10:46+11:00' to '20140311141046+11:00'

I am getting the dateTime value in the format 2014-03-11T14:10:46+11:00.
I need to change this into the format 20140311141046+11:00.
The method I am using right now is:
private string changeDateFormat()
{
DateTime dt = Convert.ToDateTime("2014-03-11T14:10:46+11:00");
return dt.ToString("yyyyMMddHHmmss");
}
It seems to be working fine but with one catch.
Instead of showing the output like 20140311141046+11:00,
the output is 20140311084046.
I think I need to pass the timezone too while converting to string. But I am blank on how to do that.
PS: This +11:00 is dynamic can can change in the input. Please suggest a generic solution/approach.
Since you parse it to DateTime you lost the offset part.
I would parse it to DateTimeOffset instead of DateTime and use K format specifier with it's format like;
var str = "2014-03-11T14:10:46+11:00";
var dto = DateTimeOffset.Parse(str);
return dto.ToString("yyyyMMddHHmmssK");
returns
20140311141046+11:00

Read string value "yyyyMMddHHmmss" in DateTime type

Here's my case.
I have a method ISO8601DateTime_Local(DateTime dto)
I cannot change its argument datatype as it will affect the entire solution.
Now I had a requirement to change the input date from 2014-03-11T14:10:46+11:00
to 20140311141046+1100
I have done it using the method below:
public string test1() {
var str = "2014-03-11T14:10:46+11:00";
var dto = DateTimeOffset.Parse(str);
return dto.ToString("yyyyMMddHHmmssK");
}
Now I have to pass this test1 as argument in ISO8601DateTime_Local()
like
DateTime dt = ISO8601DateTime_Local(test1());
Please suggest.
You can use the DateTime.ParseExact method with your format string:
DateTime dt = ISO8601DateTime_Local(DateTime.ParseExact(test1(), "yyyyMMddHHmmssK", CultureInfo.InvariantCulture, DateTimeStyles.None));
(Maybe you want to use DateTime.TryParseExact for more stability)
I'm not sure about the DateTimeStyles argument, so check the msdn if there is a more appropriate value than DateTimeStyles.None (especially DateTimeStyles.AdjustToUniversal may be interesting).

c# converting datetime in a different culture

I'm trying to convert a german DateTime value into a french DateTime object.
The value of my item called "_dateFacture" is "08.07.2015 17:23:01"
var stringdate = _dateFacture.ToString(new CultureInfo("fr-FR")); //d2 = "08/07/2015 17:23:01"
var testfinal = DateTime.Parse(stringdate, new CultureInfo("fr-FR")); // testfinal = "08.07.2015 17:23:01"
How is it possible for the object testfinal to get a value like that ?
If you had declared the datatype with each variable, instead of using var, it would have been easier to spot.
string stringdate = _dateFacture.ToString(new CultureInfo("fr-FR"));
DateTime testfinal = DateTime.Parse(stringdate, new CultureInfo("fr-FR"));
testfinal is a DateTime, not a string.
When you look at it in a debugger or view, testfinal is converted to a string using DateTime.ToString() which uses the current culture (which presumably displays dates with a dot).
What you saw is just a representation issue. They are the same DateTime values.
DateTime doesn't have any implicit format. And it doesn't have any IFormatProvider. It just a date and time values. Format is only a subject when you try to get it's textual representation.
I strongly suspect you see this in your debugger or something;
Your stringdate is a string, but your testfinal is a DateTime. If you wanna save your datetime values in your database, don't save their string representations. Put your datetime values directly to your parameterized queries.
Read: Bad habits to kick : choosing the wrong data type
if you want to store testfinal as a datetime, you just need to make sure your var is in fact a DateTime.
DateTime testfinal = DateTime.Parse(stringdate, new CultureInfo("fr-FR"));
To show it you can use a format in the ToString function:
string formattedDate = testfinal.ToString("dd.MM.yyyy HH:mm:ss");
//now your formattedDate will be "08.07.2015 17:23:01"

Formatting string to string Date Format .net 2.0

Given a string value eg "03252013" I need to convert this string to be in this format "DDMMYYYY".
Which function do you use? The result should be "25032013"
string myTestDate="03252013";
string resultDate=??
Thanks
Use DateTime.ParseExact method. Then use ToString to convert to appropriate format.
var dt = DateTime.ParseExact("03252013", "MMddyyyy", CultureInfo.InvariantCulture);
var result = dt.ToString("ddMMyyyy"); //25032013
string resultDate = DateTime.ParseExact(myTestDate, "MMddyyyy", CultureInfo.InvariantCulture).ToString("ddMMyyyy");
Simply parse then re-format:
string input = "03252013"
DateTime date = DateTime.ParseExact(input, "MMddyyyy", CultureInfo.InvariantCulture);
string resultDate = date.ToString("ddMMyyyy");
If your needs really are that simple I would consider just switching the values around...
string input = "03252013";
string output = input.Substring(2, 2) + input.Substring(0, 2) + input.Substring(4, 4);
...dont forget to validate the input before using substring (checking the length is 8 will probably be enough)
NOTE: If it is likely that your input or output formats will change then it would be more ideal to do the DateTime.Parse technique as suggested by many others. But if this is really the only situations then this method should provide better performance... slightly ;)
You, can use the Help of following code part to convert.
string resultDate = DateTime.ParseExact(myTestDate, "MMDDYYYY", System.Globalization.CultureInfo.CurrentCulture, System.Globalization.DateTimeStyles.None).ToString("DDMMYYYY");
Use this:
resultDate = DateTime.Parse(myTestDate,"d").toString();
"d" is a format provider that says to farmat the date like this: DD/MM/YYYY

Categories

Resources