This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Converting .NET DateTime to JSON
How can I convert a date value in "mm/dd/yyyy hh:mm:ss" format to "/Date(1324414956395)/" format (Json Date).
I am passing the date format "mm/dd/yyyy hh:mm:ss" into a MVC controller action method and I need to compare that to another date in JsonDate format in the code.
Thanks for help.
dt.ToUniversalTime() won't be recognised when using DateTime?. DateTime? is essentially Nullable<DateTime>. What you need to do is use the Value property to retrieve the DateTime object
dt.Value.ToUniversalTime();
You can then use the code from this post (Jeff Meatball Yang's answer, not the accepted answer) with your nullable DateTime.
(DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
Or
This superior solution from this link: Converting .NET DateTime to JSON
DateTime d1 = new DateTime(1970, 1, 1);
DateTime d2 = dt.ToUniversalTime();
TimeSpan ts = new TimeSpan(d2.Ticks - d1.Ticks);
return ts.TotalMilliseconds;
It seems that this is a bad way to compare dates, however. This is basically counting the ticks since 1970 (or something) and is going to be a pretty long and accurate number. Even if you needed to make certain that the dates matched down to a second it seems like it would be easier to convert all the universal time to mm/dd/yyyy format and then compare at that point.
Related
This question already has answers here:
Converting a String to DateTime
(17 answers)
Closed 3 years ago.
I want to convert string datetime to Datetime using C#. I am going to store datetime in sql database
The string in your example has an offset component so you can use DateTimeOffset:
var dateTimeOffset = DateTimeOffset.Parse("2012-08-16T19:20:30.456+08:00", CultureInfo.InvariantCulture);
From the linked docs:
The DateTimeOffset structure includes a DateTime value, together with
an Offset property that defines the difference between the current
DateTimeOffset instance's date and time and Coordinated Universal Time
(UTC).
just
DateTime date= DateTime.Parse(dateString);
Use DateTime.Parse("2012-08-16T19:20:30.456+08:00")
Use can use C# Interactive Windows to test it.
//string value of date
var iDate = "2012-08-16T19:20:30.456+08:00";
//Convert.ToDateTime(String)
//This method will converts the specified string representation of a date and time to an equivalent date and time value
var dateConversion1 = Convert.ToDateTime(iDate);
//DateTime.Parse()
//DateTime.Parse method supports many formats. It is very forgiving in terms of syntax and will parse dates in many different formats. That means, this method can parse only strings consisting exactly of a date/time presentation, it cannot look for date/time among text.
var dateConversion2 = DateTime.Parse(iDate);
//DateTime.ParseExact()
//ParseExact method will allow you to specify the exact format of your date string to use for parsing. It is good to use this if your string is always in the same format. The format of the string representation must match the specified format exactly.
var dateConversion3 = DateTime.ParseExact(iDate, "yyyy-MM-dd HH:mm tt", null);
//CultureInfo
//The CultureInfo.InvariantCulture property is neither a neutral nor a specific culture. It is a third type of culture that is culture-insensitive. It is associated with the English language but not with a country or region.
var dateConversion4 = DateTime.ParseExact(iDate, "yyyy-MM-dd HH:mm tt", System.Globalization.CultureInfo.InvariantCulture);
//DateTime.TryParse method
//DateTime.TryParse converts the specified string representation of a date and time to its DateTime equivalent using the specified culture - specific format information and formatting style, and returns a value that indicates whether the conversion succeeded.
DateTime dateConversion5;
DateTime.TryParse(iDate, out dateConversion5);
These are few C# methods which can be used as a conversion string to DATETIME, make sure string is a valid string so that it allows you to convert.
This question already has answers here:
Parse string to DateTime in C#
(9 answers)
Closed 3 years ago.
In the above code, I am getting the date data from SpreadsheetDocument. The exampled date cannot be converted.
But other dates can be converted.
Why cant i convert this date and how can I convert it in different way?
DateTime.FromOADate(double.Parse("05.09.1977"));
I want to convert this string to DateTime with this.
"05.09.1977 is not a valid double. It looks like it's an actual date (either MONTH.DAY.YEAR or DAY.MONTH.YEAR).
To parse it to a DateTime use either:
DateTime.ParseExact("05.09.1977", "dd.MM.yyyy", CultureInfo.InvariantCulture)
or
DateTime.ParseExact("05.09.1977", "MM.dd.yyyy", CultureInfo.InvariantCulture)
This way:
Console.WriteLine(DateTime.ParseExact("05.09.1977", "dd.MM.yyyy", CultureInfo.InvariantCulture));
Output:
05/09/1977 00:00:00
according to the
DateTime.FromOADate(Double) method doc
Returns a DateTime equivalent to the specified OLE Automation Date.
public static DateTime FromOADate (double d);
Parameters
d
Double
An OLE Automation Date value.
so, you have to pass a double as argument
but double.Parse("05.09.1977")???
how can that be a double??
that is the reason
This question already has answers here:
Converting a String to DateTime
(17 answers)
Closed 7 years ago.
I receive DateTime in this format => yyyyMMddHHmmss e.g. 20160214204032
Due to its somewhat unique format, I can't just use Convert.ToDateTime -- I tried, it didn't work.
It's easy enough to create a helper method that would parse the components of this date e.g
var year = myString.Substring(0,4);
but I'm concerned that this may have poor performance.
Can anyone think of a better way to convert a string in this format to DateTime?
You cannot set format in Convert.ToDateTime. So, use ParseExact instead.
DateTime.ParseExact("20160214204032", "yyyyMMddHHmmss",
System.Globalization.CultureInfo.InvariantCulture)
Due to its somewhat unique format, I can't just use Convert.ToDateTime -- I tried, it didn't work.
It fails because Convert.ToDateTime tries to convert from your system Date Time Format and throws exception if it can't.
using String Functions is also bad to convert to DateTime so you can do
DateTime dt = DateTime.ParseExact("20160214204032",
"yyyyMMddHHmmss",
System.Globalization.CultureInfo.InvariantCulture)
This question already has answers here:
how to give format DateTime.Date?
(7 answers)
Closed 7 years ago.
I need only the date from a DateTime class so I use DateTime.Date,
but it gives me a Full DateTime format, like this:
DateTime date = new DateTime();
date.Date
give me:
+ date {12/01/2016 00:00:00} System.DateTime
I only need the:
date {12/01/2016}
12/01/2016 00:00:00 is the same as 12/01/2016 as a value.
If you wanna get 12/01/2016 as a string representation, you can use .ToString method like;
var str = date.Date.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);
If you wanna get this 12/01/2016 as a DateTime, simply you can't. A DateTime instance always has date and time part. That's why you see it as 12/01/2016 00:00:00 on debugger even if you set it's time part to midnight.
This question already has answers here:
Parse string to DateTime in C#
(9 answers)
Closed 7 years ago.
I am trying to read date from excel using code
String ss = (String)w.Cells[2 + i, 4].Value2;
dRow[3] = DateTime.Parse(ss);
Code works when ss = "12/11/2015" but gives error
String was not recognized as a valid DateTime
when ss = "13/11/2015"
It gives error because month can not be 12 but it is taking date as month. This is what I think. Same code is working on other PC. Do I need to check my date time format or anything like date setting.
DateTime.Parse uses standard date and time format of your CurrentCulture settings by default.
Looks like your CurrentCulture has MM/dd/yyyy format as a short date format and since there is no month as 13 in Gregorian Calendar (which probably uses as a Calendar for your CurrentCulture), you get FormatExcetion.
You can use DateTime.ParseExact method to specify your format exactly like;
dRow[3] = DateTime.ParseExact("13/11/2015", "dd/MM/yyyy",
CultureInfo.InvariantCulture);
If you get this as an input and you want to parse it to DateTime, you have to know which format it has. Other than that, it can generate ambiguous scenarios.
For example; what 01/02/2015 should be parsed as? 1st February or 2nd January?
You shouldn't assume that every string you supplied perfectly parsed with DateTime.Parse method. It is not that smart.