DateTime to int [closed] - c#

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.

Related

How to validate a string is combined by year and week number in c#? [closed]

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 1 year ago.
Improve this question
How to check a string is combined by year and week number by c# code?
Like "202153",here "53" is the 53th week in 2021,and the week number cannot over 53 in 2021,
so,"202154" is not valid.
Thanks everyone for your suggestions, this is indeed an ambiguous question.I confirmed with my leader that our current system only needs to consider that the number of weeks does not exceed 53.
try this. you can change last line as per logic.
string date1 = "202154";
int year = Convert.ToInt32(date1.Substring(0, 4));
int weekNo = Convert.ToInt32(date1.Substring(4, 2));
//Validate week
DateTimeFormatInfo dfi = DateTimeFormatInfo.CurrentInfo;
DateTime date = new DateTime(year, 12, 31);//Get last date of year
Calendar cal = dfi.Calendar;
int weekOfYear = cal.GetWeekOfYear(date, dfi.CalendarWeekRule, dfi.FirstDayOfWeek);//Get last week of the year.
bool answer = weekNo > weekOfYear?false:true;

How to convert DateTimeOffset ToString("o") offset set to be 00 [closed]

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.

Regional decimal seperator input [closed]

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;
}

addition of two month and year in asp.net with c# [closed]

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);

convert date mm/dd/yyyy to yyyy-mm-dd date itself in the format [closed]

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 9 years ago.
Improve this question
I am trying not to format ToString(). I need the date itself in the format.i cannot do to string
scenario:
string str = "02/11/2006";
DateTime dt = new DateTime();
dt = Convert.ToDateTime(str);
now dt is in the format mm/dd/yyyy, but i need in the format yyyy-mm-dd
hope i add some sense..huh!
Not sure what you mean. If you have the date in a DateTime variable it is an universal representation of the date. If you wish to convert it back to string you can use the following:
string str = "02/11/2006";
DateTime dt = new DateTime();
dt = Convert.ToDateTime(str);
var date = dt.ToString("yyyy-MM-dd");
You are confusing the DateTime Structure with its string presentation.
if you want to convert a string with a custom DateTime format you have to use DateTime.ParseExact method
dt = DateTime.ParseExact(str ,"mm/dd/yyyy",CultureInfo.InvariantCulture);
if you want a particular string presentation of a DaTime you have to use a format string
dt.ToString("yyyy-mm-dd")
A DateTime has no format (it's only data). Only String representation have one. Add this to your code :
String newFormatedDate = dt.ToString("yyyy-MM-dd");
I think you just need to know about the ToString overloads:
DateTime newDate = DateTime.ParseExact("15/01/2001", "dd/MM/yyyy", null);
Console.WriteLine(newDate.ToString("yyyy-MM-dd"));

Categories