Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a string "5-13-2013"
I want to make it look like "051313"
All three fields (month,day,year) must have 2 digits, if it only has one , I need to add a zero in front
any easy way to do this?
You can convert your datetime to multiple formats like
string date = yourdateTime.ToString("dd/mm/yyyy");
string date = yourdateTime.ToString("dd/mm/yyyy HH:mm");
string date = yourdateTime.ToString("dd/mm/yyyy HH:mm:ss");
string date = yourdateTime.ToString("mmddyy"); // your desired
To know what these mean you need to read here.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
(so it looks like this "15.05-2019")but I don't want to remove the dot or the dash.How do I do this in C#
You could parse it to a DateTime and then format it back.
var result = DateTime.ParseExact("15.May-2019", "dd.MMMM-yyyy", System.Globalization.CultureInfo.InvariantCulture)
.ToString("dd.MM-yyyy");
This is useful if you need to handle dates with any month in them.
Note that the MMMM will work for long month names, but if you're dealing with short names MMM would be the way to go (for May the short and long names are the same). You can also use the overload of ParseExact that takes multiple formats if you need to handle both.
var str = "15.May-2019";
str = str.Replace("May", "05")
To replace part of a string you can do:
var date = "15.05-2019";
date = date.Replace("05", "May");
Console.WriteLine(date);
output : "15.May-2019"
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I need to get all dates in any formats from an string.
for example:
string text = "aaaaa25/01/2019 21:22:56 dfgdfv cvcvbcvb 25/02/18 2:12 asdfd";
I need a methods that extract all the dates
25/01/2019 21:22:56,
25/02/18 2:12
as DateTime.
I try by Regex:
var splitter = new Regex("[0-9]{2}/[0-9]{2}/[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}");
foreach (Match m in splitter.Matches(str))
{
string dateString = m.Groups[0].Value;
DateTime dt = DateTime.ParseExact(dateString, "MM/dd/yy HH:mm:ss", null);
}
But I need to write any formats and there is a lot options.
I looking for method that detect any format of datetime.
/\d{1,2}\/\d{1,2}\/(?:\d{2}){1,2} \d{1,2}:\d{2}(:?:\d{2})?/gm
For a few pieces:
\d{1,2} matches 1 or two digits.
(?:\d{2}){1,2} matches a group of two digits once or twice. (therefore, 2 or 4 digits)
(?:\d{2}:)? allows the seconds to be optional
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I m very new to C# and have some issue. I've a project where I need to change the date when School starts at 0700 am only not at 1200 am. I tried something like this but doesn't work
System.TimeSpan timeDiff = TimeSpan.FromHours(.292);
DateTime presentDate = (Convert.ToDateTime(sysDateTime)).Subtract(timeDiff);
I used this way in VB6 where I just substract .292 from current dateTime to change the date at 0700 am instead of 1200 am but dont know how do I achieve the same in C#.
You can try with
DateTime d = (Convert.ToDateTime(sysDateTime)).Subtract(new TimeSpan(5, 0, 0));
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have decimal values in the database. I want to show the decimal value in it. How to convert decimal values into it.
int vatprice;
dt.Load(cmd.ExecuteReader());
vatprice = Convert.ToInt32(dt.Rows[0][5].ToString());//getting error here
As per your requirement, round decimal up to 2 place using following way :
1) Use Math.Round().
var vatprice= Convert.ToDecimal(Math.Round(dt.Rows[0][5], 2));
2) second way is
var vatprice=Convert.ToDecimal(dt.Rows[0][5].ToString ("#.##"));
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I want to get the previous chars from a certain char in a string.
For example: myString = "26+", so I want to get 26 without +. How can I do this?
Use Substring and IndexOf.
string str = "26+";
string requiredString = str.Substring(0, str.IndexOf('+'));
strings are immutable in C# and you have to assign the results to string itself or some other string.
The function String.Substring will do what you need.
yourString.Substring(0, keepCharactersBeforeHere);