i have a string like "14-Nov-2014" , i want to convert this string to this 14.11.2014 format.
after converting i want to add 14 days to above date.
given date is not Datetime format.
Old date="14-Nov-2014"
new date=14.11.2014
is there any way to do in c#?
Assuming,
var myString = "14-Nov-2014";
First parse the string, most likely using DateTime.ParseExact. Assuming a few things about the format you have, you could do the following. Note you most likely should specify the proper culture for the third argument:
var dateTime = DateTime.ParseExact(myString, "dd-MMM-yyyy", null);
Then you can add 14 days to it easily:
var dateTime = dateTime.AddDays(14);
To get a new string in a different format just use ToString with a format string. For example:
var myNewString = dateTime.ToString("d.MM.yyyy");
Related
I am new to C# and I have a string like "2021-06-14 19:27:14:979". Now I want to have only the time "19:27:14:979". So do I parse the string to a specific DateTime format and then convert it back to a string or would you parse or cut the string itself?
It is important that I keep the 24h format. I don't want AM or PM.
I haven't found any solution yet. I tried to convert it to DateTime like:
var Time1 = DateTime.ParseExact(time, "yyyy-MM-dd HH:mm:ss:fff");
var Time2 = Time1.ToString("hh:mm:ss:fff");
But then I lost the 24h format.
Your code is almost working, but ParseExact needs two additional arguments and ToString needs upper-case HH for 24h format:
var Time1 = DateTime.ParseExact("2021-06-14 19:27:14:979", "yyyy-MM-dd HH:mm:ss:fff", null, DateTimeStyles.None);
var Time2 = Time1.ToString("HH:mm:ss:fff");
Read: https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings#uppercase-hour-h-format-specifier
Instead of passing null as format provider(means current culture) you might want to pass a specifc CultureInfo, for example CultureInfo.CreateSpecificCulture("en-US").
You can just split it at the blank and take the last part like this
var timestamp = "2021-06-14 19:27:14:979";
var timePart = timestamp.Split(' ')[1];
in your case that seems easier than parsing into a DateTime and back into a string.
I have a dateTime string as follows "\"2017-09-20T02:55:15.000Z\"". I want to parse it in C#.
DateTime gpsddt
string date = "\"2017-09-20T02:55:15.000Z\"";
var result = DateTime.TryParse(date, out gpsddt);
result is false. I don't know how to parse the above string. I got this string from a gpsd daemon. I can't find any format specifier that matches this datetime string here
The string is currently "2017-09-20T02:55:15.000Z", but it needs to be 2017-09-20T02:55:15.000Z to parse correctly. As #Chetan suggested in the comment, you need to strip out the " characters.
Add this line before parsing.
date = date.Replace("\"", "");
Alternatively, the DateTime.TryParseExact allows you to specify the source format. However it does require you to consider a lot more factors such as culture and style to use as well.
result = DateTime.TryParseExact(date, "\"\\\"\"yyyy-MM-ddTHH:mm:ss.fffZ\"\\\"\"", new CultureInfo("en-AU"), DateTimeStyles.AssumeUniversal, out gpsddt);
It is just a simple matter of doing this:
DateTime gpsddt;
string date = "\"2017-09-20T02:55:15.000Z\"";
var result = DateTime.TryParse(date.Trim('"'), out gpsddt);
I want to convert the date into something different. I give input date, it will convert into that format.
My code
var dt = erd.StartDate.Value.ToShortDateString();
var format = String.Format("{MMM/D/yyyy}", dt);
Here it is showing an error like input string is not correct format.
Help me to find out the issue?
Simply rewrite it like this (I'm assuming the erd.StartDate.Value datatype is datetime)
string result = erd.StartDate.Value.ToString("MMM/dd/yyyy");
or using "D" is equivalent to "dddd, MMMM d, yyyy"
string result = erd.StartDate.Value.ToString("D");
you need to use small d for representing Date.
you should not convert the Date into String before applying the custom Format.
Try This:
var dt = erd.StartDate.Value;
var format =dt.ToString("MMM/d/yyyy");
To solve your error, try the below code:
var dt = erd.StartDate.Value;
var format = String.Format("{0:MMMM/dd/yyyy}", dt);
You need to specify the parameter index to use. In the above code, we are telling String.Format to use parameter 0 to be formatted as a DateTime.
DateTime formatting has many types. Please see here for the different things you can do.
DateTime dt = DateTime.Now
dt.Date is created to 31.10.2012 00:00:00 .it is created to dd.mm.yyyy format but i need dd/mm/yyyy. Can i use: return new DateTime(d.Year, d.Month, d.Day, 0, 0, 0); it will create to me dd/mm/yyyy solution?Please dont translate String.i need datetime...
The DateTime struct doesn't store any formatting information internally. If you want to output the DateTime instance as a formatted string, you just need to call ToString() with the proper format string:
var date = DateTime.Now;
var formattedString = date.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);
If you need more information on exactly which specifiers to use in your format string, check out:
MSDN - Custom Date and Time Format Strings
Just the way to convert to string, DateTime itself has no format:
var result = DateTime.Now.Date
.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);
var dt = DateTime.Now;
var stringDt = dt.Date.ToString("dd/MM/yyyy");
In you case you can simply use :
dt.ToString("dd/MM/yyyy");
Anyway there al the string format you can use with DateTime : Here.
System.DateTime does not have any format. You can view its string representation in format.
Try this
Console.WriteLine(DateTime.Now.Date.ToString("dd'/'MM'/'yyyy"));
DateTime, numeric types and most other types do not store their values in a formatted way. Rather they store their data using a binary representation. If you want to display this data to the user, you must convert it to a string. This conversion involves formatting the data.
string formattedDate = DateTime.Now.ToString("dd/MM/yyyy",
CultureInfo.InvariantCulture);
Or
Console.WriteLine("Date = {0:dd/MM/yyyy}", DateTime.Now);
Console.WriteLine converts the date into a string in order to write it to the console.
DateTime structure always has the Date and Time stored in it. If you need to extract the date alone as text you can do the following.
var date = DateTime.Now.ToString("d");
Console.WriteLine(date);
This will print the date as in the format as specified by the culture set in the system. The list of standard datetime format strings supported by dotnet framework can be found here
I have a date that shows up as 10/18/2011 3:12:33 PM
How do I get only the time portion of this datetime?
I am using C#.
I tried:
string timeval = PgTime.ToShortTimeString();
but that did not work as Intellisense only showed ToString();
Assuming that
DateTime PgTime;
You can:
String timeOnly = PgTime.ToString("t");
Other format options can be viewed on MSDN.
Also, if you'd like to combine it in a larger string, you can do either:
// Instruct String.Format to parse it as time format using `{0:t}`
String.Format("The time is: {0:t}", PgTime);
// pass it an already-formatted string
String.Format("The time is: {0}", PgTime.ToString("t"));
If PgTime is a TimeSpan, you have a few other options:
TimeSpan PgTime;
String formattedTime = PgTime.ToString("c"); // 00:00:00 [TimeSpan.ToString()]
String formattedTime = PgTime.ToString("g"); // 0:00:00
String formattedTime = PgTime.ToString("G"); // 0:00:00:00.0000000
If you want a formatted string, just use .ToString(format), specifying only time portions. If you want the actual time, use .TimeOfDay, which will be a TimeSpan from midnight.
DateTime PgTime = new DateTime();
var hr = PgTime.Hour;
var min = PgTime.Minute;
var sec = PgTime.Second;
//or
DateTime.Now.ToString("HH:mm:ss tt") gives it to you as a string.
Don't now nothing about a class named PgTime. Do now about DateTime, though.
Try
DateTime instance = DateTime.Now ; // current date/time
string time = instance.ToString("t") ; // short time formatted according to the rules for the current culture/locale
Might want to read up on Standard Date and Time Format Strings and Custom Date and Time Format Strings
In C# 10 you can use TimeOnly.
TimeOnly date = TimeOnly.FromDateTime(PgTime);