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 7 years ago.
Improve this question
Computers in my country do not accept decimals with a '.' they only accept ','
How do I write a program which would accept 12.5 from the console as a decimal number?
Use CultureInfo. If you are sure that using decimal dot is a standard in your country then use your country's CultureInfo. You can simply check if it is.
// Change kk-KZ to your culture code
decimal dec = decimal.Parse(str, new CultureInfo("kk-KZ"));
Or you can use InvariantCulture and NumberStyles.Any:
decimal dec = decimal.Parse(str, NumberStyles.Any, CultureInfo.InvariantCulture);
I will implement the second one as I don't know where you are from.
Here it is in an console application and it works for me:
public static void Main()
{
string str;
str = Console.ReadLine();
decimal dec = decimal.Parse(str, NumberStyles.Any, CultureInfo.InvariantCulture);
double db = double.Parse(str, NumberStyles.Any, CultureInfo.InvariantCulture);
Console.WriteLine(dec.ToString("F2", CultureInfo.InvariantCulture));
Console.WriteLine(db.ToString("F2", CultureInfo.InvariantCulture));
return;
}
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I wanna convert MM/YY formatted string date to mm-yy DateTime. And set as the value for rad masked edit box. But it returns to me
"String was not recognized as a valid DateTime."
I tried with
DateTime dt = DateTime.ParseExact("11/17", "MMyy", CultureInfo.InvariantCulture);
for eg, I want to convert 03/16 and set value of radmasked edit box masked as MMyy as 03-16
Why do you expect this to work at all?
DateTime dt = DateTime.ParseExact("11/17", "MMyy", CultureInfo.InvariantCulture);
You get a string 11/17 and try to parse it with a format that doesn't contain any delimiters.
This works:
DateTime dt = DateTime.ParseExact("11/17", "MM/yy", CultureInfo.InvariantCulture);
If you want to convert it to a string with this format: MMyy:
string result = dt.ToString("MMyy", CultureInfo.InvariantCulture);
Since it's not clear, if you want this instead: MM-yy
string result = dt.ToString("MM-yy", CultureInfo.InvariantCulture);
It's working for 11/17,12/17. but not in the case of 3/12 etc i.e when
a month is a single digit.
You haven't mentioned that it's possible that the month has a single digit, however:
DateTime dt = DateTime.ParseExact("3/17", "M/yy", CultureInfo.InvariantCulture);
This will work for you.
DateTime dt = DateTime.ParseExact("11/17", "MM/yy", CultureInfo.InvariantCulture);
Then you can convert it to your desired format
string formattedDate = dt.ToString("MM-yy");
In case you want to change delimeter '/' into '-' only, i.e. if you have no need in DateTime temporary value, you can just Replace:
string source = "11/17";
// 11-17: changing '/' to '-'
string result = source.Replace('/', '-');
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
DateTimeOffset.Now = {2/22/2018 10:32:50 AM -06:00} been converted to
xmlformatTime = "2018-02-22T10:33:18.2670639-06:00", , in the format need to remove and expected o/p as 2018-02-22T10:33:18.00-06:00.
18.2670639 needs to change as 18.00
DateTimeOffset PassDate = DateTimeOffset.Now;
int minutes = PassDate.Minute;
int seconds = PassDate.Second;
var offset = PassDate.Offset;
var xmlformatTime = PassDate.ToString("o");
Console.WriteLine(xmlformatTime)
when tried with below option.
var xmlformatTime = PassDate.ToString("'MM'-'dd'T'HH':'mm':'ss'.00'K");
From MSDN:
The "O" or "o" standard format specifier corresponds to the "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffffffzzz" custom format string for DateTimeOffset values
So use
var xmlformatTime = PassDate.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.00'zzz");
instead to change the milliseconds to zeros.
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 6 years ago.
Improve this question
I have a file with a few million lines in them.
Every line starts like this:
2016/04/05 11:20:43.293
I would like a regex (or other option?) to get all the lines that fall between two times. (for example between 11:20 and 11:25)
Also, if it's possible to match one or more words in those lines, that would be helpfull as well. However, perhaps a regex isn't the best way to go then?
You could use DateTime.TryParseExact and File.ReadLines with this LINQ query:
string format = "yyyy/MM/dd HH:mm:ss.fff";
DateTime dt;
var relevantLines = File.ReadLines(path)
.Where(l => l.Length >= format.Length
&& DateTime.TryParseExact(l.Substring(0, format.Length), format, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.None, out dt)
&& dt.TimeOfDay >= start && dt.TimeOfDay <= end);
First you have to use a Regex:
Regex TimePattern = new Regex("\\d{2}:\\d{2}.\\d{3}");
Parse the matches to datetime and check if the time is valid:
foreach (Match M in TimePattern.Matches(FILECONTENT))
{
DateTime Dt = Convert.ToDateTime(M.groups[1]));
//Now you can check if the time "Dt" is between 11:20 and 11:25
}
To compare the time you could use (like described in Is there BETWEEN DateTime in C# just like SQL does?):
public static bool Between(DateTime input, DateTime date1, DateTime date2)
{
return (input > date1 && input < date2);
}
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 7 years ago.
Improve this question
How to covert DateTime to int
DateTime now = DateTime.Now;
int va = now.Year;
// error here
int vd = int.Parse(mYear.Value.ToShortDateString());
result = Convert.ToString(va - vd);
Input string was not in a correct format
Assuming mYear is of Type DateTime - Just do it like you did with now:
int vd = mYear.Year;
If it's a string like "2014", use something like:
DateTime.ParseExact(mYear, "yyyy", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None);
Not sure what Parse is going to do with escape chars.
// The example displays the following output:
// Displaying short date for en-US culture:
// 6/1/2009 (Short Date String)
// 6/1/2009 ('d' standard format specifier)
# ElGavilan commented to the same effect while I was posting.
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
addition of two month and year in asp.net with c#.
if i select any one Month/year: like march/2014 and add(+) 12 month,
so it should be give the February/2014.
In this we can see Loan Period is: 12 (month) and below we can see loan start from month: 07(july/2014) so end of the load should be 06/2015. and the both month are in textbox it means they are string.
DateTime dt = new DateTime(2013, 1, 1);
dt.AddYear(1);
dt.AddMonths(2);
//Date is 2014, 3 (March), 1
Alternatively if you wish to substrat years and months you can use:
dt.AddYear(-1);
dt.AddMonths(-1);
//Date is 2013, 2 (February),1
Here you will not get 02/2015 if you add 12 months in 03/2014. You will get 03/2015 in result as shown below.
var inputString = "march/2014";
DateTime dt = DateTime.ParseExact(inputString, "MMMM/yyyy", CultureInfo.InvariantCulture);
var result = dt.AddMonths(12).ToString("MMMM/yyyy");
Will result in => "march/2015"
It seems that yous should add -1, not 12 months (if you want to get February from March):
String fromDate = "march/2013";
// result == "February/2013"
String result = DateTime
.ParseExact(fromDate, "MMMM/yyyy", CultureInfo.InvariantCulture)
.AddMonths(-1)
.ToString("MMMM/yyyy", CultureInfo.InvariantCulture);
In case that you want to add a year and two months (and so your example is incorrect)
String fromDate = "march/2013";
// result == "May/2014"
String result = DateTime
.ParseExact(fromDate, "MMMM/yyyy", CultureInfo.InvariantCulture)
.AddYears(1)
.AddMonths(2)
.ToString("MMMM/yyyy", CultureInfo.InvariantCulture);