Easiest way to convert GMT date to DateTime object [closed] - c#

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 5 years ago.
Improve this question
I am getting GMT DateTime as string input. For example
SampleDate = "20170221T085258.732 GMT"
Now, I want to convert this to datetime object. What is the best way of doing this conversion?

You just need to use ToLocalTime() Then you can change it to whatever timezone you care about.
DateTimeOffset.Parse(SampleDate).ToLocalTime();

var offset = new Date().getTimezoneOffset();
To remove the GMT and time zone, change the following line:
document.write(d.toString().replace(/GMT.*/g,""));

Hi try this code using DateTime.ParseExact()
string SampleDate=""20170221T085258.732 GMT";
DateTime dateObject = DateTime.ParseExact(SampleDate,"ddd MMM dd yyyy HH:mm:ss 'GMT'zzz", System.Globalization.CultureInfo.InvariantCulture);
For more info heres the link for DateTime.ParseExact MSDN: https://msdn.microsoft.com/en-us/library/w2sa9yss(v=vs.110).aspx

Below code worked for me. Date contain some unwanted characters like "T",".","GMT" , once I removed those, it started working..
But I feel that, there has to be some better solution for this.
//I can write a regular expression to keep only numeric values and avoid this replacements...
SampleDate = "20170221T085258.732 GMT"
SampleDate = SampleDate.Replace("GMT", "")
SampleDate = SampleDate.Replace("T", "")
SampleDate = SampleDate.Replace(".", "")
Dim dateObject As DateTime = DateTime.ParseExact(SampleDate.Trim(), "yyyyMMddHHmmssfff", System.Globalization.CultureInfo.InvariantCulture)

Related

How to get the date from the given string 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 2 years ago.
Improve this question
I only need date from this string Wed, 02/05/2020 - 12:31 what format should I use whenever I am using
{MM/dd/yyyy} I am getting the same value if there is any other way please let me know
item.changed=Wed, 02/05/2020 - 12:31
`if (ListRecord.checkresponse == "Response Letters")
{
string checkresponse = item.changed;
string issueDate = string.Format("{0:MM/dd/yyyy}", checkresponse);
}`
To use date formatting, you need to start with a DateTime object, not a string.
So in your scenario you'll have to parse your string as a DateTime, then format it out again to a different string format. There's no way to directly format string -> string, all the logic about formatting is in the DateTime class.
Here's an example:
string changed = "Wed, 02/05/2020 - 12:31";
var checkresponse = DateTime.ParseExact(changed, "ddd, MM/dd/yyyy - HH:mm", CultureInfo.InvariantCulture);
string issueDate = string.Format("{0:MM/dd/yyyy}", checkresponse);
Console.WriteLine(issueDate);
Demo: https://dotnetfiddle.net/bhNImo
(Alternatively, since what you mainly want to do here is strip the first and last parts of the string and keep the date part, you could use a regular expression do to this from the string, but overall it's probably more reliably to use date parsing and formatting.)

Convert Time with milliseconds to dateTime in c# [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I am trying to convert Time string to DateTime but can't.
Example: Time: 12:05:45.458 with milliseconds need to convert to time.
Any solution?
DateTime.ParseExact should solve this
DateTime result = DateTime.ParseExact("12:05:45.458", "HH:mm:ss.fff", CultureInfo.InvariantCulture);
The DateTime.ParseExtract will parse the given time and convert to DateTime object with date as current date.
var dateTime = DateTime.ParseExact("12:05:45.458", "HH:mm:ss.fff", System.Globalization.CultureInfo.InvariantCulture);
Console.WriteLine(dateTime);
You are looking for a Timespan.Parse() here. Since you do not have any date component, it is not possible to unambiguously convert to a DateTime instance.
Simple solution:
string time = "12:05:45.458";
DateTime dateTime = DateTime.ParseExact(time, "HH:mm:ss.fff",
CultureInfo.InvariantCulture);

how to return DateTime instead of string along with 24hrs date/time 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 6 years ago.
Improve this question
By default DateTime type in C# gave time in AM/PM format, but I would like to return 24 hrs time format and same time I want to return this DateTime type instead of string like below,
DateTime localTime = DateTime.Now;
string timeString24Hour = localTime.ToString("dd-MM-yyyy HH:mm:ss", CultureInfo.InvariantCulture) ;
instead of "string timeString24Hour" I want "DateTime timeString24Hour"
How to achieve this???
DateTime localTime = DateTime.Now;
That is a DateTime type in 24-hour format. A DateTime is a number representing all of the milliseconds since January 1, 1970. From it, you can convert to any part of time, or any string of time. If you want to use a DateTime, though - there is no inherent formatting to it until you need one by using
DateTime.Hours/DateTime.DayOfWeek/DateTime.Subtract(antoherDateTime).TotalSeconds
if/when you need time in a 24- hour format. Just use:
DateTime.ToString("HH:mm")
from here: How to format DateTime to 24 hours time?
By default DateTime is dispalyed in AM/PM in your culture.
Because the developement environment always uses the current culture to display DateTime
But that's only how it is being displayed. The value within DateTime is the same - culture independent.
By th way "mm/dd/yyyy HH:mm:ss" is invalid becasue mm are minutes and MM is for month
I presume you want something like String.Format :
DateTime localTime = DateTime.Now;
string timeString24Hour = String.Format("{0:dd-MM-yyyy HH:mm:ss.fff}", localTime);
Or shorter:
string timeString24Hour = String.Format("{0:dd-MM-yyyy HH:mm:ss.fff}", DateTime.Now);
You can find informations here : MSDN: Custom Date and Time Format Strings

DateTimeFormat issue [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have a string like below
1/1/1970 12:00:00 AM
I want it in the yyyy-MM-dd format (in this case 1970-01-01)
My parsing code is
var actualDate = DateTime.ParseExact(actualValue,"MM/dd/yyyy HH:mm:ss tt",CultureInfo.InvariantCulture).ToString("yyyy-MM-dd");
But I keep getting the error, that the string is not recognized as a valid date time.
I looked at my variable actualValue and it is of type DateTime, so am thinking that the problem is with the format MM/dd/yyyy HH:mm:ss tt ,What is wrong with this?
First, you shouldn't be storing or fetching dates as text in the DB.
To your specific issue, however, MM and dd are the two-digit variety of month and day. Obviously your date text doesn't use the two-digit-only variety, so use M and d. Also, HH is on a 24-hour clock. Using tt, which is AM/PM, would imply not having a 24 hour clock, so you would want to use hh instead.
For more, look at MSDN for custom date/time formatting.

Converting convert the datetime in dd/MMM/yyyy to string in dd/MM/yyyy format 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
I have a input variable with DateTime datatype in "03-Jan-2010" format I want to convert that in to a String type of dd/MM/yyyy format.How can this be done using C#.
Just use DateTime.ToString(string) method like;
datetime.ToString(#"dd\/MM\/yyyy");
Remember, "/" format specifier has a special meaning in custom date and time formatting as replace me current culture's or specified culture date separator. If your CurrentCulture's DateSeparator is already /, you can use it without escaping like;
datetime.ToString("dd/MM/yyyy");
or you can provide IFormatProvider which has / as a DateSeparator as a second parameter to your .ToString() method (ex: InvariantCulture ) like;
datetime.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);
string ddd= DateTime.Now.ToString("dd-MM-yyyy");
Visit: http://forums.asp.net/t/1820419.aspx?How+to+convert+dd+mm+yyyy+format+date+into+yyyy+dd+mm+in+C+
Please try following demo you will get what you want
string myDate = "03-Jan-2010";
DateTime dt = DateTime.Now;
DateTime.TryParseExact(myDate, "dd-MMM-yyyy", null, System.Globalization.DateTimeStyles.None, out dt);
string formattedDate = dt.ToString("dd/MM/yyyy");
Console.Write("Formatted Date : {0}", formattedDate);
Console.ReadKey();
Hope it works for you...

Categories