Format DateTime.Now - c#

A peculiar request this one.
I am looking to compare two times in C#.
I have:
DateTime systemDate = DateTime.Now.AddMinutes(-15);
//which I presume subtracts 15 minutes from the current time
I am then reading in an ftp file from a Fedora server containing a date and time that I am reading in as a stream and converting to a DateTime
2011-03-17 09:00:18.000000000 +0000
I then parse this:
compareDate = DateTime.Parse(streamer);
My Question therefore is how do I format systemDate to be in the same format as the one I have parsed in compareDate? I am aware you can format DateTime but all of the sites I have seen so far dont have it in this format.
Thank you in advance, feel free to comment if you think I have missed anything :)

You can do it like this,
DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");

No need to format systemDate, because you already parsed the date from the FTP server into a DateTime. You can just compare systemDate with compareDate.

just use var systemDate = DateTime.UtcNow.AddMinutes(-15); for your original one, then you just
if (DateTime.Equals(systemDate, compareDate))
{
//...
}

Related

Parse a date from an export of a lotus notes base

In a C# application, I have to parse an xml file which is an export of a lotus notes database.
The exports contains dates in this format :
<noteinfo noteid='6706' unid='6B6A3ADD41061773C12580F2004E8EB3' sequence='6'>
<created>
<datetime dst='true'>20170329T161803,39+02</datetime>
</created>
<modified>
<datetime>20171108T100439,39+01</datetime>
</modified>
<revised>
<datetime>20171108T100439,38+01</datetime>
</revised>
<lastaccessed>
<datetime>20171108T100439,38+01</datetime>
</lastaccessed>
<addedtofile>
<datetime dst='true'>20170329T163711,21+02</datetime>
</addedtofile>
</noteinfo>
I have to extract these dates into a .Net Datetime value.
However, I don't get what's this format. Trying to parse the date fails:
var created = DateTime.Parse("20170329T161803,39+02");
Throws
String was not recognized as a valid DateTime
How to parse this date format ?
The first part of the date, before the comma, is obvious. The second part is less. +02 probably match the GMT+02 timezone, but I don't get the 39
PS: I don't have control on the exports
Assuming ,39 are the milliseconds
var created = DateTime.ParseExact("20170329T161803,38+02", "yyyyMMddTHHmmss,ffz", CultureInfo.InvariantCulture);
Reference: DateTime.ParseExact
Try
var created = DateTime.ParseExact("20170329T161803,39+02".Remove(15), "yyyyMMdd'T'HHmmss",CultureInfo.InvariantCulture);
I'm not sure what the ,39+02 is in terms of a time - zone perhaps? I trimmed it off, but if you can describe what it is maybe it can be parsed
Edit:
Assuming that's milliseconds and timezone:
var created = DateTime.ParseExact("20170329T161803,39+02", "yyyyMMdd'T'HHmmss','ffz",CultureInfo.InvariantCulture);

How to convert string in "06-03-2016T06:42:44.252Z" format to datetime?

I have the string "06-03-2016T06:42:44.252Z" and I would like to convert it to a datetime.
The top answer from this post suggested using:
DateTime.Parse(string, null, System.Globalization.DateTimeStyles.RoundtripKind);
This works if I format my date like "2016-06-03T06:42:45Z" but not "06-03-2016T06:42:44.252Z"
How can I convert "06-03-2016T06:42:44.252Z" to datetime properly?
Thank you very much for your time. Please let me know if I am being unclear or if you need anything else from me.
I could not find another question on stack asking how to convert from this exact format and could not apply the strategies from them to my case. I can transform my string to match those used in the example I linked but I am losing a bit of precision and adding more work in the process. I would like to leave this question up and unmarked as a duplicate in hopes of finding a means of parsing my date format or confirming that it cannot be done.
Top answer you linked pointing to ISO8601 format but your string is not on that format. Since the Z in your input indicates a UTC time, I would suggest using DateTime.ParseExact, where you can specify the exact format you want and preserve the UTC time with AdjustToUniversal style:
var dt = DateTime.ParseExact(
"06-03-2016T06:42:44.252Z",
"MM-dd-yyyyTHH:mm:ss.fffZ",
CultureInfo.InvariantCulture,
DateTimeStyles.AdjustToUniversal);
Console.WriteLine(dt); // June 03 2016 06:42 (...)
Console.WriteLine(dt.Kind); // Utc

DateTime problem when day <= 12

I've looked around a lot and short of writing a horrible chunk of code to manipulate the string, I'd like to ask if anyone knows a nice way of sorting it:
I have a bunch of date strings in cells that I'm pulling out such as:
03/05/2011
27/05/2011
31/05/2011
03/05/2011
09/05/2011
31/05/2011
etc.
While I'm reading any entires where the day can be construed as a month - i.e. entries 1, 4 and 5 above - it gets put in as a DateTime with the day and month swapped.
For example, 03/05/2011 gets read in as a DateTime "05/03/2011 00:00:00"
The others are all read and nicely provide me with a simple string of "27/05/2011".
I'm getting this info from Excel, using
((Excel.Range)worksheet.Cells[rowCount, 3]).Value.ToString()
If I try Value2 as with my other lines, it reads those odd dates as things like "40607" but again, will read the other dates normally.
If you use the DateTime.ParseExact function to convert a string to a DateTime object, you can specify the specific format used by your dates (which looks like "day/month/year") without having to do any string manipulation whatsoever.
Example:
var dateString = "03/05/2011";
var format = "dd/MM/yyyy";
var date = DateTime.ParseExact(dateString, format, CultureInfo.InvariantCulture);
More information on custom Date and Time format strings can be found here.
EDIT: Try using the DateTime.FromOADate method to convert the value returned by the Range.Value2 property to a DateTime object, e.g. something like this:
var dateTime = DateTime.FromOADate(((Excel.Range)worksheet.Cells[rowCount, 3]).Value2);
DateTime.ParseExact Method converts the specified string representation of a date and time to its DateTime equivalent using the specified format and culture-specific format information.
The format of the string representation must match the specified format exactly.
String dateString = "15/06/2008";
String format = "dd/MM/yyyy";
DateTime result =
DateTime.ParseExact(dateString, format, CultureInfo.InvariantCulture);
That sounds like a localization problem. Try setting your locale implicititly. For example in WPF application it's something like:
System.Threading.Thread.CurrentThread.CurrentCulture =
new System.Globalization.CultureInfo("en-US");
I have a bunch of date strings in cells that I'm pulling out such as:
No, you don't. You have a mix of strings that look like dates and dates that look like strings. This is an Excel issue, not a C# issue.
Not sure if you are creating the spreadsheet, or if you are getting it from somewhere else. But it the problem is that Excel attempt to parse text as it is entered in the cell. In this case, it is making some wrong decisions about the dates it finds.
If you enter a date like "03/05/2011", Excel will (incorrectly) parse it as March 5th, 2011, and store that as a numeric date code (40607). It then applies a date formatting to the cell (it uses m/d/yyyy on my machine).
If you enter a date like "31/05/2011", Excel can't parse it as a date, and it stores it as text.
To prove this, select the cells and go to Edit > Clear > Formats. All the "bad dates" will just show as numbers, all the rest will stay looking like dates.
You have a few choices:
Fix the data before its entered into Excel (prepend everything with a ' so its all entered as text, or make sure to create the spreadsheet on a machine that has the right date settings.)
Don't use the .Value.ToString() from Excel, just use .Text. This will ignore the bad parsing that Excel did, and should give you a consistent text value (from both types) that you can ParseExact with C#, per the other answers.
(2) is a lot easier, and if the spreadsheets already exist, may be your only choice.
The problem is because your Dates are being read as american culture or similar.
If you use the following you can specify the format you expect your dates to be in:use
DateTime result;
if(DateTime.TryParseExact("dd/MM/yyyy", out result))
{
// Got an English date
}

.NET DateTime to BizTalk DateTime

I have date in this format "1999-05-31T13:20:00.000-05:00" I want to add some hours or days to it . BizTalk expects same datetime format how can i pass it as Datetime in that format ? No string . Date time with same format as source date.
You should be able to do something like this to get it into a DateTimeOffset object. After that you can call whatever methods you want on it.
DateTimeOffset dateTime = DateTimeOffset.Parse( "1999-05-31T13:20:00.000-05:00" );
To get the value back just use a formatting string.
dateTime.ToString( "O" ); //this should be the same format as you started with
Here are some other options http://msdn.microsoft.com/en-us/library/az4se3k1.aspx
Here is a link to the DateTimeOffset structure
http://msdn.microsoft.com/en-us/library/bb351654.aspx
I hope this helps.
The DateTime object is format-independent (for the most part). So whether or not it starts in the format you list or not doesn't matter. You can always get it back into that format (using the ToString("o") function). But that's as a string (when format matters).
After a quick search, it looks like you must be talking about string format, even though you said no string. So the other answer or the ToString("o"); part of mine is relevant.

datetime.parse and making it work with a specific format

I have a datetime coming back from an XML file in the format:
20080916 11:02
as in
yyyymm hh:ss
How can I get the datetime.parse function to pick up on this? Ie parse it without erroring?
DateTime.ParseExact(input,"yyyyMMdd HH:mm",null);
assuming you meant to say that minutes followed the hours, not seconds - your example is a little confusing.
The ParseExact documentation details other overloads, in case you want to have the parse automatically convert to Universal Time or something like that.
As #Joel Coehoorn mentions, there's also the option of using TryParseExact, which will return a Boolean value indicating success or failure of the operation - I'm still on .Net 1.1, so I often forget this one.
If you need to parse other formats, you can check out the Standard DateTime Format Strings.
Thanks for the tip, i used this to get my date "20071122" parsed, I needed to add datetimestyles, I used none and it worked:
DateTime dt = DateTime.MinValue;
DateTime.TryParseExact("20071122", "yyyyMMdd", null,System.Globalization.DateTimeStyles.None, out dt);

Categories

Resources