How to convert string into date time with milliseconds in C# - c#

How do I convert this string into a date time with milliseconds
03/04/2019 15:16:57.73
The format is day/month/year hour:minutes:seconds.milliseconds.
I tried it like this:
var format = "dd/MM/yyyy HH:mm:ss.fff";
var year = dateTime.Substring(0, 4);
var month = dateTime.Substring(4, 2);
var day = dateTime.Substring(6,2);
var hour = dateTime.Substring(8, 2);
var minute = dateTime.Substring(10, 2);
var seconds = dateTime.Substring(12, 2);
var miiliseconds = dateTime.Substring(14, 2);
var stringDate = $#"{day}/{month}/{year} {hour}:{minute}:{seconds}.{miiliseconds}";
var transactioDateTime = DateTime.ParseExact(stringDate, format, CultureInfo.InvariantCulture);
But I get an error of
Additional information: String was not recognized as a valid DateTime.
Can you please help me with this? Thank you.

Seems like you don't need substrings at all.
Just put your date string right into ParseExact and you will get what you want.
However the error there is because you have different milliseconds format: it expects you will put three digits there but you passed only two:
var dateTime = "03/04/2019 15:16:57.73";
var format = "dd/MM/yyyy HH:mm:ss.ff"; - this is the fix.

Related

0 Character Problem in the beggining DateTime

I got every part of date in the code that you can see below. But the problem is if we consider today's date I need day and month as 02 not as 2.
I need that 0 char in the beggining. How can I manage it?
DateTime dategift = DateTime.Now;
var year = dategift.Year.ToString();
var month = dategift.Month.ToString();
var day = dategift.Day.ToString();
var hour = dategift.Hour.ToString();
var min = dategift.Minute.ToString();
var sec = dategift.Second.ToString();
use the Zero placeholder
string day = dategift.Day.ToString("00");
or the "dd" custom format specifier
string day = dategift.ToString("dd");
If you're wanting to format the date, you can do so without pulling out all the various values as strings.
Something like:
DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");

Get TimeSpan from HH:mm:ss string

I'm trying to get a TimeSpan from "24:30:00" string so I can define cacheOptions in C# but I'm getting 24 days instead of 24 hours.
string cacheExpirationTime = "24:00:00";
var cacheOptions = new MemoryCacheOptions()
{
ExpirationScanFrequency = TimeSpan.Parse(cacheExpirationTime, CultureInfo.InvariantCulture)
};
I also tried without using CultureInfo, but it didn't work.
Which is the proper way to do this?
24 hours is 1 day, so you should format it as such.
string cacheExpirationTime = "1.00:00:00";
var cacheOptions = new MemoryCacheOptions()
{
ExpirationScanFrequency = TimeSpan.Parse(cacheExpirationTime, CultureInfo.InvariantCulture)
};
If you want to use the format hh:mm:ss. You need to specify the format, here "hh:mm:ss" is used. hh is for hours, mm for minutes, and ss for seconds.
Be aware that 24:00:00 could not be used because it's not a valid value for a TimeSpan object. The largest possible value for a TimeSpan object is 23:59:59, so any value greater than that will cause an OverflowException to be thrown.
string cacheExpirationTime = "23:59:59";
string format = "hh\\:mm\\:ss";
var cacheOptions = new MemoryCacheOptions()
{
ExpirationScanFrequency = TimeSpan.ParseExact(cacheExpirationTime, format, CultureInfo.InvariantCulture)
};
By default, TimeStamp assumes the input string represents a time duration in the format days.hours:minutes:seconds so you need to use a custom format string with TimeSpan.ParseExact() method like this:
string cacheExpirationTime = "24:00:00";
var cacheOptions = new MemoryCacheOptions()
{
ExpirationScanFrequency = TimeSpan.ParseExact(cacheExpirationTime, #"h\:mm\:ss", CultureInfo.InvariantCulture)
};

Convert String (2015-FEB-17) to Date (20150217) format

I tried the following but couldn't get it
string s= "2015-FEB-17";
//I want it to be converted to date format as
date = "20150217"
//I tried doing as follows but didn't work
var myDate = DateTime.ParseExact(dt, "yyyy-MMM-dd", CultureInfo.InvariantCulture);
why not shorten it since you are using the .ToString("yyyyMMdd") you are dropping the time portion of the new value
string sDateStr = "2015-FEB-17";
var newDateFrmt = Convert.ToDateTime(sDateStr).ToString("yyyyMMdd");
20150217 becomes the expected answer based on the format..
not to be redundant this approach can also be taken
string sDateStr = "2015-FEB-17";
var someDate = DateTime.ParseExact(sDateStr, "yyyy-MMM-dd", CultureInfo.InvariantCulture);
var newDateFrmt = someDate.ToString("yyyyMMdd");
ParseExact turns the string into a DateTime. You then need to format the DateTime as a string.
string s= "2015-FEB-17";
DateTime myDate = DateTime.ParseExact(s, "yyyy-MMM-dd", CultureInfo.InvariantCulture);
string result = myDate.ToString("yyyyMMdd"); // now it's "20150217"
Also, you were missing dashes in the ParseExact format string.
If your application only handles US-style dates with no internationalization, it's best to specify CultureInfo.InvariantCulture.
Try
var myDate = DateTime.ParseExact(s, "yyyy-MMM-dd", CultureInfo.InvariantCulture);
var q = myDate.ToString("yyyyMMdd");
or just
var q = DateTime.ParseExact(s, "yyyy-MMM-dd", CultureInfo.InvariantCulture)
.ToString("yyyyMMdd");

Convert string to datetime and remove the time part from the date

I have a date returned from database which includes even the time. i want to remove the time part of the string and send only the date. my code is as given below
DateTime Var = new DateTime();
Var = Convert.ToDateTime(Dset.Tables[1].Rows[i]["Date"]);
Var = Var.ToShortDateString();
DateTime Var = Convert.ToDateTime(Dset.Tables[1].Rows[i]["Date"]).Date; //only date part
string date = Var.ToShortDateString();
it will store only date in DateTime object
Var = Var.Date;
time will be 00:00:00
or you can store it as string:
var dateString = Var.ToShortDateString();
you can custom your date string format by using DateTime.ToSting("your format") method.
then the code will be like this.
DateTime Var = new DateTime();
Var = Convert.ToDateTime(Dset.Tables[1].Rows[i]["Date"]);
Var = Var.ToString("yyyy-MM-dd");
you can also use SubString() method to gain the date part of the datetime string.

asp.net DateTime manipulation

I have various strings of type: 7/12/2012 12:02:39 AM
and would like to convert them all to just 7/12/2012 12:00:00 AM
Basically the date needs be the same, just the time must be set to 12:00:00 AM for all.
What is the best way to approach that? Except just looking for " " and replacing with 12:00:00 AM
First, parse them to a DateTime. Then you can use the Date property(0h) and parse it back to a String by using DateTime.ToString:
var oldDate = DateTime.Parse("7/12/2012 12:02:39 AM");
var usCulture = System.Globalization.CultureInfo.CreateSpecificCulture("en-US");
var newDateStr = oldDate.Date.ToString( usCulture );
Custom Date and Time Format Strings
only just noticed that 12 AM is midnight not highnoon
You can create new DateTime instance via constuctor:
var newDate = new DateTime(oldDate.Year, oldDate.Month, oldDate.Day, 12, 0, 0);
or by adding 12 hours to the date part of the DateTime(0h):
var newDate = oldDate.Date.AddHours(12);
Parse them as DateTime
Write them back to string using yourDateTime.Date.ToString(#"G", CultureInfo.CreateSpecificCulture("en-us"))
use DateTime.Parse and specify the CulterInfo
DateTime mydat = DateTime.Parse(myString, CultureInfo.InvariantCulture);

Categories

Resources