How to convert strings to DateTime C# [duplicate] - c#

This question already has answers here:
Parse string to DateTime in C#
(9 answers)
Closed 7 years ago.
I have 3 separate strings with the following format:
01-29-2016: a date, picked from a bootstrap datepicker
1:00am start time, picked from a dropdown, format could also be e.g. 10:00pm
2:30am end time, picked from a dropdown, format could also be e.g. 11:30pm
Using the above strings I need to construct 2 DateTime properties that represent the time range, something like below:
2016-01-29 02:30:00
2016-01-29 01:00:00
I need the DateTime properties so I could update datetime database fields

You can combine both your time parts with date part respectively and use ParseExact method with MM-dd-yyyyH:mmtt format like;
var date = "01-29-2016";
var ts1 = "1:00am";
var ts2 = "2:30am";
var dt1 = DateTime.ParseExact(date + ts1, "MM-dd-yyyyH:mmtt", CultureInfo.InvariantCulture);
// 29.01.2016 01:00:00
var dt2 = DateTime.ParseExact(date + ts2, "MM-dd-yyyyH:mmtt", CultureInfo.InvariantCulture);
// 29.01.2016 02:30:00

Related

How convert the string of "06/22/2019 00:00:00" to a valid DateTime type in format of 2019/06/22 without the part of hour and minute and second [duplicate]

This question already has answers here:
How to remove time portion of date in C# in DateTime object only?
(43 answers)
Closed 3 years ago.
I am doing an asp.net mvc project and I need to convert the string of "06/22/2019 00:00:00" to a valid DateTime type in format of 2019/06/22 without the part of hour and minute and second
You can use DateTime.ParseExact, here is an example :
http://net-informations.com/q/faq/stringdate.html
Finally, it should look like this :
string s = "06/22/2019 00:00:00";
DateTime myDate = DateTime.ParseExact(s, "MM/dd/yyyy HH:mm:ss",System.Globalization.CultureInfo.InvariantCulture);
Debug.WriteLine(myDate.ToString("MM/dd/yyyy"));
You can do this:
var dateString = "06/22/2019 00:00:00";
var datePart = dateString.Split(' ')[0];
var date = DateTime.Parse(datePart);
Though remember that DateTime will still have a default value for the time (12:00 AM), if you want the Date part only from the object, use date.Date which will return an instance with the default time (mentioned earlier).
DateTime contains default Time even if you access DateTime.Date. You can achieve format of date by converting Date into string.
Something like,
DateTime myDate = DateTime.ParseExact("06/22/2019 00:00:00", "MM/dd/yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);
string dateInFormat = $"{myDate.Year}/{myDate.Month}/{myDate.Day}";
POC : .net Fiddle
You convert the string to a DateTime object and then to display just the date portion you can use ToShortDateString like this:
var myDateTime = DateTime.Parse( "06/22/2019 00:00:00") //presumably use a variable here instead.
var date = myDateTime.ToShortDateString();
How you want to display this can be done using the CultureInfo part as shown here: https://learn.microsoft.com/en-us/dotnet/api/system.datetime.toshortdatestring?view=netframework-4.8

How to convert string into datetime format c# [duplicate]

This question already has answers here:
how to conver date string from one format to another format in c#?
(4 answers)
Closed 5 years ago.
I am trying to convert string 201702070001 into DateTime format like 2017-02-07 00:01
For this I tried like as below. I also followed the tutorial http://www.csharp-examples.net/string-format-datetime/. but not able to solve the problem.
string dateTime = "201702070001";
IFormatProvider culture = new System.Globalization.CultureInfo("en-US", true);
DateTime dt2 = DateTime.Parse(dateTime, culture, System.Globalization.DateTimeStyles.AssumeLocal);
You have to convert it using the right format. Like this:
DateTime dt2 = DateTime.ParseExact("201702070001", "yyyyMMddHHmm", CultureInfo.InvariantCulture);
You should first convert the string into DateTime object and then using the custom date formatting strings to print the DateTime, based on your needs.
Below is an example
var dateStr = "201702070001";
DateTime dt = DateTime.ParseExact(dateStr, "yyyyMMddHHmm", null);
Console.WriteLine(dt.ToString("yyyy-MM-dd HH:mm"));
I am using the default FormatProvider. Depending on the locale settings on your computer, it might or might not work for you
You have provided 2 examples: 201702070001 and 20170302254. They are not consistent with each other. The first one fits yyyyMMddHHmm but the other does not. You will have to find the proper format string that you need. You can reference the correct format specifiers from Custom Date and Time Format Strings

C# - UTC automatically convert to 12h system [duplicate]

This question already has answers here:
DateTime.Value.ToString(format) gives me 12 hour clock
(4 answers)
Closed 6 years ago.
I'm working with a project and one of its functional requirement is to create a datetime range in which I will select a set of data based on that.
This range should take today's datetime starting from 8:00:00 to 18:00:00, and then I want to convert the format to be yyyy-MM-ddThh:mm:ssZ, so I'm doing the following to approach that:
DateTime fromTime = DateTime.Now;
TimeSpan ts = new TimeSpan(8,0,0);
fromTime = fromTime.Date + ts;
string fromTimeFormat = fromTime.ToString("yyyy-MM-ddThh:mm:ssZ");
DateTime toTime = DateTime.Now;
TimeSpan tss = new TimeSpan(18, 0, 0);
toTime = toTime.Date + tss;
string toTimeFormat = toTime.ToString("yyyy-MM-ddThh:mm:ssZ");
The problem is that, the toTimeFormat is being converted to the 12h system, so when I use it later it's being considered as 6:00 AM.
Any ideas please?
Because you are using hh specifier which is for 12-hour clock format.
You need to use HH specifier which is for 24-hour clock format.
string toTimeFormat = toTime.ToString("yyyy-MM-ddTHH:mm:ssZ");
And you can simplify your code as;
string fromTimeFormat = DateTime.Today.AddHours(6).ToString("yyyy-MM-ddThh:mm:ssZ");
string toTimeFormat = DateTime.Today.AddHours(18).ToString("yyyy-MM-ddTHH:mm:ssZ");

how to combine date and time in c# [duplicate]

This question already has answers here:
Combine two datetime variables into one (up to seconds precision)
(4 answers)
Closed 9 years ago.
I have 2 values:
var dt1 = dtFromDate.Value;
var tm1 = tmFromTime.Value;
dt1 = 12/5/2013 12:00:00 AM
tm1 = 11/5/2013 9:00:00 AM
i want to make datetime as : 12/5/2013 9:00:00 AM
how can it be possible?
You can take the date part of dt1 by accessing Date, the time part of tm1 by accessing TimeOfDay and then combine them using +:
dt1.Date + tm1.TimeOfDay
You can create a new DateTime using overloaded constructor:
DateTime dt = new DateTime(
dt1.Year,
dt1.Month,
dt1.Day,
tm1.Hour,
tm2.Minute,
tm2.Second,
tm2.Millisecond);
You can use that code, using Date property to get just date part and Time property to get just time part:
var dt1 = DateTime.Parse("12/5/2013 12:00:00 AM"); // this is just a sample date
var tm1 = DateTime.Parse("11/5/2013 9:00:00 AM"); // this is just a sample date
var newDate = dt1.Date.Add(tm1.TimeOfDay); // the code to use
DateTime object is immutable so in order to get the date from one DateTime object, but the time from another - you must create new DateTime object.
There is two ways for doing it:
Create new DateTime with constructor overload:
DateTime date1 =
new DateTime(dt1.Year, dt1.Month, dt1.Day, tm1.Hour, tm1.Minute, tm1.Second);
Parse string to DateTime:
var dateToParse =
String.Concat(dt1.ToString("yyyy.MM.dd ", CultureInfo.InvariantCulture),
tm1.ToString("HH:mm:ss", CultureInfo.InvariantCulture));
var date1 =
DateTime.ParseExact(dateToParse, "yyyy.MM.dd HH:mm:ss", CultureInfo.InvariantCulture);

extract the date part from DateTime in C# [duplicate]

This question already has answers here:
How to remove time portion of date in C# in DateTime object only?
(43 answers)
Closed 9 years ago.
The line of code DateTime d = DateTime.Today; results in 10/12/2011 12:00:00 AM. How can I get only the date part.I need to ignore the time part when I compare two dates.
DateTime is a DataType which is used to store both Date and Time. But it provides Properties to get the Date Part.
You can get the Date part from Date Property.
http://msdn.microsoft.com/en-us/library/system.datetime.date.aspx
DateTime date1 = new DateTime(2008, 6, 1, 7, 47, 0);
Console.WriteLine(date1.ToString());
// Get date-only portion of date, without its time.
DateTime dateOnly = date1.Date;
// Display date using short date string.
Console.WriteLine(dateOnly.ToString("d"));
// Display date using 24-hour clock.
Console.WriteLine(dateOnly.ToString("g"));
Console.WriteLine(dateOnly.ToString("MM/dd/yyyy HH:mm"));
// The example displays the following output to the console:
// 6/1/2008 7:47:00 AM
// 6/1/2008
// 6/1/2008 12:00 AM
// 06/01/2008 00:00
There is no way to "discard" the time component.
DateTime.Today is the same as:
DateTime d = DateTime.Now.Date;
If you only want to display only the date portion, simply do that - use ToString with the format string you need.
For example, using the standard format string "D" (long date format specifier):
d.ToString("D");
When comparing only the date of the datatimes, use the Date property. So this should work fine for you
datetime1.Date == datetime2.Date
DateTime d = DateTime.Today.Date;
Console.WriteLine(d.ToShortDateString()); // outputs just date
if you want to compare dates, ignoring the time part, make an use of DateTime.Year and DateTime.DayOfYear properties.
code snippet
DateTime d1 = DateTime.Today;
DateTime d2 = DateTime.Today.AddDays(3);
if (d1.Year < d2.Year)
Console.WriteLine("d1 < d2");
else
if (d1.DayOfYear < d2.DayOfYear)
Console.WriteLine("d1 < d2");
you can use a formatstring
DateTime time = DateTime.Now;
String format = "MMM ddd d HH:mm yyyy";
Console.WriteLine(time.ToString(format));

Categories