Reputation: 184
My string Is like 10130060/151015/0017164
want to convert the bold part of string into date
i.e 15/10/2015
Upvotes: 1
Views: 829
Reputation: 916
I would split the string bit for bit. Here is a function that returns a string array. Each string would have the same length.
public static string[] SplitBitforBit(string text, int bitforbit)
{
int splitcount = Convert.ToInt32(RoundAt(text.Length / bitforbit, 0, 0));
char[] allChars = text.ToCharArray();
string[] splitted = new string[splitcount];
int iL = 0;
for (int i = 0; i != splitted.Length; i++)
{
splitted[i] = null;
for (int j = 0; j != bitforbit; j++)
{
splitted[i] += allChars[iL];
iL++;
}
}
return splitted;
}
The RoundAt-method will round up automatically if you set Position and startUp to 0:
public static double RoundAt(double Number, int Position, int startUp)
{
double Up = Math.Abs(Number) * Math.Pow(10, Position);
double temp = Up;
double Out;
while (Up > 0)
{
Up--;
}
Out = temp - Up; //Up
if (Up < (Convert.ToDouble(startUp) - 10) / 10)
{ Out = temp - Up - 1; } //Down
if (Number < 0)
{ Out *= -1; }
Out /= Math.Pow(10, Position);
return Out;
}
Example:
string yourString = "171115";
string[] splitted = SplitBitforBit(yourString, 2);
int day = Convert.ToInt32(splitted[0]);
int month = Convert.ToInt32(splitted[1]);
int year = 2000 + Convert.ToInt32(splitted[2]);//!!! just for years since 2000
DateTime yourDate = new DateTime(year, month, day);
Console.WriteLine(yourDate);
Console.ReadLine();
Upvotes: 0
Reputation: 98868
It is not completely clear but when you say with bold part, if you mean always between first /
and second /
characters, you can split your string with /
and you can parse it to DateTime
with ParseExact
method.
var s = "10130060/151015/0017164";
var dt = DateTime.ParseExact(s.Split('/')[1], "ddMMyy", CultureInfo.InvariantCulture);
Console.WriteLine(dt); // 15/10/2015
But remember, as others commented, since you need to use yy
format specifier, this specifier uses supplied culture Calendar.TwoDigitYearMax
property.
This property allows a 2-digit year to be properly translated to a 4-digit year. For example, if this property is set to 2029, the 100-year range is from 1930 to 2029. Therefore, a 2-digit value of 30 is interpreted as 1930, while a 2-digit value of 29 is interpreted as 2029.
For InvariantCulture
, it's set to 2029
. That's why your 15
will be parsed as 2015
but 76
will be parsed as 1976
.
By the way, my solution does not work if your single day and month numbers does not have a leading zero like 51015
or 15515
. In such cases, you need to use d
and/or M
format specifiers instead.
thanx for the answer but the problem is this data is feeded by the user at run time and it will be in the format ddmmyy , the year part is fixed it will be for this century only.
First of all, mm
and MM
specifiers are not same. mm
specifier is for minutes but MM
specifier is for months. All custom date and time specifiers are case sensitive.
If you always wanna parse your two digit years in 21st century, you can Clone
a culture that uses Gregorian Calendar as a Calendar
property like InvariantCulture
, set it's TwoDigitYearMax
property to 2099
, and use that cloned culture when you parse your string.
var clone = (CultureInfo)CultureInfo.InvariantCulture.Clone();
clone.Calendar.TwoDigitYearMax = 2099;
var dt = DateTime.ParseExact(s.Split('/')[1], "ddMMyy", clone);
Your 151015
will parse as 15/10/2015
and your 151076
parsed as 15/10/2076
.
Upvotes: 3