Making a DateTime? from two strings for date and time [duplicate] - c#

This question already has answers here:
How to combine two strings (date and time) to a single DateTime
(9 answers)
Closed 7 years ago.
My input I recieve two strings:
string1 = "12/15/2015"
string2 = "22:45"
How can I combine them to have a DateTime? value.

Use this.
DateTime dt = Convert.ToDateTime(string.Format("{0} {1}",string1,string2));

Related

C#: DateTime form string [duplicate]

This question already has answers here:
String was not recognized as a valid DateTime " format dd/MM/yyyy"
(13 answers)
datetime.parse and making it work with a specific format
(2 answers)
Closed 2 years ago.
So I have this `string:
string time = "20201006 10:42:42.925"
Which look like valid time format.
And this is what I have try:
DateTime dt1 = DateTime.Parse(time);
And got this error: String was not recognized as a valid DateTime.
Use TryParse Method, eg
dateTime dt1;
if DateTime.TryParse(time, out dt1)
{
// all good
} else{
// no good
}

converting datetime to string with T07:00:00.0000000Z [duplicate]

This question already has answers here:
Given a DateTime object, how do I get an ISO 8601 date in string format?
(18 answers)
Closed 4 years ago.
I am attempting to query table storage by generating the following query:
var date = new DateTime(1954, 9, 7);
var timequery = TableQuery.GenerateFilterConditionForDate("Timestamp", QueryComparisons.LessThanOrEqual, date.ToString());
I am getting a bad request when simply doing date.ToString()
The string that I need would be in the following format: 1954-09-07T07:00:00.0000000Z
How do I convert a regular DateTime to be a string in the specified format?
Var date = new DateTime(1954,7,0,0,0,0,DateTimeKind.Utc);
Var stringDate = date.ToUniversalTime().ToString(“o”);
Gives you the required result as
1954-09-07T00:00:00.0000000Z

Datetime comparison [duplicate]

This question already has answers here:
How to compare DateTime in C#?
(9 answers)
Closed 7 years ago.
My program is reading data from SQL Server like this:
if (!reader.IsDBNull(4))
{
message.WazneDo = reader.GetDateTime(4);
}
This code read datetime from SQL Server. Then I will load it to datagridview:
WypozyczZwrocDb _dost1 = new WypozyczZwrocDb();
Global.listWypozyczZwroc = _dost1.PokazZar();
Global.fMain.Tabela.DataSource = Global.listWypozyczZwroc;
How to compare with today's date? I want change the row color.
I tried:
DateTime dzis = DateTime.Now;
if(Tabela.Rows[Tabela.CurrentCell.RowIndex].Cells[4].Value > dzis)
if(Tabela.Rows[Tabela.CurrentCell.RowIndex].Cells[4].Value > dzis.ToString())
DateTime.Compare() will allow you to use comparative operators in a conditional statement based on the int returned.
DateTime.Compare() msdn

How can i subtract a datetimepicker from current date in C# .net? [duplicate]

This question already has answers here:
How to convert date format to DD-MM-YYYY in C#
(16 answers)
Closed 7 years ago.
TimeSpan result = DateTime.Today.Subtract(birthDaydateTimePicker.Value);
resultTextBox.Text = result.ToString();
I want the result to show as dd/mm/yyyy format.
This should work, your subtracting dates looks fine assuming your parameter is a date value
{
TimeSpan result = DateTime.Today.Subtract(birthDaydateTimePicker.Value);
resultTextBox.Text = result.ToString("dd/MM/yyyy");
}
Subtracting dates
Changing the format of a date

Convert month string to int format [duplicate]

This question already has answers here:
How to parse a month name (string) to an integer for comparison in C#?
(15 answers)
Closed 8 years ago.
I have the following string "Dec".
How can I convert this string to the month number 12?
I tried this:
DateTime.ParseExact("Dec", "MMMM", null).Month
This doesn't work.
You can also use ParseExact method. i.e,
string month = "Dec";
int MonthDigit = DateTime.ParseExact(month, "MMM", CultureInfo.InvariantCulture).Month;
Simply use "MMM":
DateTime.ParseExact("Dec", "MMM", null).Month
also best use a specific culture, so its consisten with the input:
DateTime.ParseExact("Dec", "MMM", System.Globalization.CultureInfo("en-us")).Month
The long version MMMM refers to a full month name as originaly posted here How to parse a month name (string) to an integer for comparison in C#?

Categories