How to order by time and time only in C#? - asp.net-mvc

I have this recorded in SQL Server:
1- startTime (datetime): 5/2/2009 08:30 (brazilian time format: d/m/y)
2- startTime (datetime): 4/2/2009 14:30 (brazilian time format: d/m/y)
My application just records time... the date it's SQL that generates by itself be getting the date of today.
When I ask VS 2008 to order this datetime fields, it returns me that code #2 is before #1, because 4/2/2009 comes before 5/2/2009.
But, actually I want it to order by time only and ignore the date.
Is it possible??
Thanks!!
André

from d in dates
orderby d.TimeOfDay
select d;
or
dates.OrderBy(d => d.TimeOfDay);
Note: This will work as long as this is plain LINQ and not LINQ-to-SQL. If you're using LINQ-to-SQL to query your database, you'll need something that will translate to SQL.

You might also try fixing your app so it always saves the same base date with the time (like '01/01/1900' or whatever) and then you do not have to do all these slow and inefficient date stripping operations every time you need to do a query.
Or as Joel said, truncate or strip off the date portion before you do the insert or update.

Well, you can't really store a datetime without a date, but you could just store the total seconds as an double (using #florian's method).
You'd have to add a second method to convert this back to a date in your object, if you still need a date, such as:
public class BusinessObjectWithDate
{
private string _someOtherDbField = "";
private double _timeInMS = 0; // save this to the database
// sort by this? in sql or in code. You don't really need this
// property, since TimeWithDate actually saves the _timeInMS field
public double TimeInMS {
get { return _timeInMS; }
}
public DateTime TimeWithDate { // sort by this too, if you want
get { return (new DateTime(1900,1,1).AddMilliseconds(_timeInMS)); }
set { _timeInMS = value.TimeOfDay.TotalMilliseconds; }
}
}
var f = new BusinessObjectWithDate();
MessageBox.Show( f.TimeWithDate.ToString() ); // 1/1/1900 12:00:00 AM
f.TimeWithDate = DateTime.Now;
MessageBox.Show( f.TimeWithDate.ToString() ); // 1/1/1900 1:14:57 PM
You could also just store the real date time, but always overwrite with 1/1/1900 when the value gets set. This would also work in sql
public class BusinessObjectWithDate
{
private DateTime _msStoredInDate;
public DateTime TimeWithDate
{
get { return _msStoredInDate; }
set {
var ms = value.TimeOfDay.TotalMilliseconds;
_msStoredInDate = (new DateTime(1900, 1, 1).AddMilliseconds(ms));
}
}
}

Try this in your sql:
ORDER BY startTime - CAST(FLOOR(CAST(startTime AS Float)) AS DateTime)

Related

Flutter/Dart DateTime parsing UTC and converting to local

I am trying to parse a UTC Date string to DateTime and then parse it to local, however I am having troubles with converting it to the local time. In the UK it should be plus one, however when I print .isUtc it returns as false.
This is what I have now:
print(widget.asset.purchaseDate);
DateTime temp = DateTime.parse(widget.asset.purchaseDate);
print(temp.toLocal());
I/flutter (5434): 2020-05-07 21:29:00
I/flutter (5434): 2020-05-07 21:29:00.000
You need to indicate a timezone to DateTime.parse, otherwise it assumes local time. From the dartdoc:
An optional time-zone offset part, possibly separated from the
previous by a space. The time zone is either 'z' or 'Z', or it is a
signed two digit hour part and an optional two digit minute part.
Since you know your string represents UTC, you can tell the parser by adding the Z suffix.
var temp = DateTime.parse(widget.asset.purchaseDate + 'Z');
print(temp.isUtc); // prints true
While #Richard Heap's answer stands I'd like DateTime.parseUtc() to exist.
If there will be a day when dart allows static extension methods here is an implementation that would work:
extension DateTimeExtension on DateTime {
static DateTime parseUtc(String formattedDate) => DateTime.parse('${formattedDate}z');
static DateTime? tryParseUtc(String? formattedDate) {
if (formattedDate != null) {
return DateTime.tryParse('${formattedDate}z');
}
return null;
}
}
Currently you'd have to use it with the extension classes name, like so:
DateTimeExtension.parseUtc(someDate)
or
DateTimeExtension.tryParseUtc(someOtherDate)
I think the latter is more useful where someOtherDate is nullable

Attempting to map dates to index in ElasticSearch

I am using ElasticSearch and attempting to create an index with a class that has a dynamic type property in it. This property may have strings or dates in it. When indexing, I have been using the code below:
dynamic instance = MyObject.GetDynamicJson();
var indexResponse = client.Index((object) instance, i=>i
.Index("myIndex")
.Type("MyObject")
);
Here's the code for GetDynamicJson().
MyObject has only Name and Value as properties. (apologies, I've had issues in the past with Elastic choking on json without all the quotes, which I have escaped with \ characters):
String json = "{ \"Name\":\" + Name + "\",\"DateValue\":\"";
try {
var date = DateTime.parse(Value);
json += DateTime.ToString("yyyy/MM/dd HH:mm:ss Z") + "\", \"Value\":\"\"}";
} catch { //If the DateTime parse fails, DateValue is empty and I'll have text in Value
json += "\",\"Value\":\"" + Value + "\"}";
}
return json;
For some reason it doesn't seem to like the string in DateValue and I definitely don't know why it's leaving out that property entirely in the error:
For whatever reason, ElasticSearch is completely dumping the DateValue property, doesn't seem to see the DateValue property at all.
I'm getting the error:
{"name":"CreatedDate","value":"2017-11-07T13:37:11.4340238-06:00"}
[indices:data/write/bulk[s][p]]"}],"type":"class_cast_exception","reason":"org.elasticsearch.index.mapper.TextFieldMapper cannot be cast to org.elasticsearch.index.mapper.DateFieldMapper"},"status":500
Later note: I have changed the index creator method to update the mapping. I added a third field to the Object, so now it has properties: Name, Value, DateValue:
public static void CreateRecordsIndex(ElasticClient client)
{
client.CreateIndex("myIndex", i => i
.Settings(s => s
.NumberOfShards(2)
.NumberOfReplicas(0)
)
.Mappings(x => x
.Map<MyObject>(m => m.AutoMap())));
}
Now, it is successfully mapping and creating a property each time, but it still seems to drop the property I am sending it in the json. It just sets them all to the default datetime: "dateValue": "0001-01-01T00:00:00". This is strange, because when making the dynamic instance I send to Elastic, I use only the MyObject.GetDynamicJson() method to build it. I no longer get the mapping error, but Elastic still seems oblivious to "dateValue":"some date here" in the object when it is set.
OK, I got rid of the dynamic object type (ultimately I wasn't actually getting data from the json method, I had a typo and Elastic was getting the original object directly - it's a wonder it was still handling it). So I let Elastic do the parse using its mapping. In order to do that, I first updated MyObject to include multiple properties, one for each type the incoming property could be (I am only handling text and dates in this case). For the DateValue property of MyObject, I have this:
public DateTime DateValue {
get
{
try
{
return DateTime.Parse(Value);
} catch
{
return new DateTime();
}
}
set
{
try {
DateValue = value;
} catch
{
DateValue = new DateTime();
}
}
}
Now, if Value is a date, my DateValue field will be set. Otherwise it'll have the default date (a very early date "0001-01-01T00:00:00"). This way, I can later search both for text against that dynamic field, or if a date is set, I can do date and date range queries against it (technically they end up in two different fields, but coming from the same injested data).
Key to this is having the index mapping setup, as you can see in this method from the question:
public static void CreateRecordsIndex(ElasticClient client)
{
client.CreateIndex("myIndex", i => i
.Settings(s => s
.NumberOfShards(2)
.NumberOfReplicas(0)
)
.Mappings(x => x
.Map<MyObject>(m => m.AutoMap())));
}
In order to recreate the index with the updated MyObject, I did this:
if (client.IndexExists("myIndex").Exists)
{
client.DeleteIndex("myIndex");
CreateRecordsIndex(client); //Goes to the method above
}

C# The DateTime represented by the string is not supported in calendar System.Globalization.GregorianCalendar

I Apologies now, but I'm not great with dates.
I'm passing a date parameter into a MVC Controller(ASP.net)
however sometimes (its happned twice) it does not like a particular time of the day and throws the following error:
Exception: mscorlib The DateTime represented by the string is not supported in calendar System.Globalization.GregorianCalendar.
I've had this on the two following values:
Query: dateTime=13%2f03%2f2017+13%3a08
Query: dateTime=12%2f03%2f2017+21%3a07
Using Moment to try and format
function addNewMeeting(date) {
var currentDate = moment().format("DD/MM/YYYY%20HH:mm").toString();
$("#appEventPlaceholder").load("/Meeting/AddEvent?dateTime=" + currentDate,
function () {
$('#eventModal').modal('show');
}
);
};
public ActionResult AddEvent(string date = null, string resourceId = null)
{
..}
I guess i'm doing something wrong with formatting the date? Could someone show me what I should be doing please?
Thank you in advance.
try this Convert.toDateTime(string)

How do I arrange date time in following format

Hi I am working C# MVC project. I have got date like this
string datetime = frmcollection["txtTo"].ToString()
Here datetime variable contains date and time in following format : 06/05/2014 10:25:39
Now I need to above datetime in int, so i replaced all /, :, and space.
So now i have following result :
int datetime = 0;
datetime = intdatetime
so here datetime variable has following reuslts : 6052014102539
So what I need here is, I need to store int time in different format like this : 2014060514102539. so basically i need to rearrange position of inttime.
How can i do this ??
string datetime = frmcollection["txtTo"].ToString();
// your date format that is coming from form collection...
string yourDateFormat = "MM-dd-yyyy HH:mm:ss";
// convert string to date time
DateTime newDate = DateTime.ParseExact(datetime, yourDateFormat, null);
// change its format and convert it to string
string newDateStr = newDate.ToString("yyyy/MM/dd HH:mm:ss");
and use your "string to int" method...
Create a method that takes your string, parses it to a date and returns a weird datetime int. Something like this:
public int ParseDateToWeirdInt(string date)
{
//Error checking omitted
var d = DateTime.Parse(date);
var stringThatWillBecomeAnInt = "";
stringThatWillBecomeAnInt = d.Year.ToString();
stringThatWillBecomeAnInt += d.Month.ToString();
stringThatWillBecomeAnInt += d.Date.ToString();
stringThatWillBecomeAnInt += d.TimeOfDay.Hours.ToString();
stringThatWillBecomeAnInt += d.TimeOfDay.Minutes.ToString();
stringThatWillBecomeAnInt += d.TimeOfDay.Seconds.ToString();
return int.Parse(stringThatWillBecomeAnInt);
}
You should probably use a StringBuilder instead of concatenating the string and a recommendation would be to turn the method into an extension method. Also note that the method needs much better error handling (the date parse could fail, the int parse could fail etc.).
Thanks guys for helping me out. I found this solution as easy for me .
datetime = Convert.ToDateTime(datetime ).ToString("yyyy/dd/MM HH:mm:ss");
so now i have datetime in format i need. Now i can convert to int.
Just adding this answer as a cleaner solution
string datetime = frmcollection["txtTo"].ToString();
string newDateTime;
DateTime theDateTime;
if (DateTime.TryParse(datetime, out theDateTime))
{
newDateTime = theDateTime.ToString("yyyy/dd/MM HH:mm:ss");
}
else
{
// tell user they have entered the date in wrong format
}

datetime utc handling with DateTimeZoneHandling

i am trying to figure out why my dates on the client are different than my dates on the server... down below i have a breezeconfig class that i thought would allow my server side "unspecified" dates to be serialized as "utc"... however a date of "2011-08-11" is still being serialized as "2011-08-11T00:00:00.000" which i believe is missing the "Z" at the end in order to signify utc... any ideas? thank you
public class BreezeConfig : Breeze.WebApi.BreezeConfig
{
protected override Newtonsoft.Json.JsonSerializerSettings CreateJsonSerializerSettings()
{
var ret = base.CreateJsonSerializerSettings();
ret.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Utc;
return ret;
}
}
Setting ret.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Utc; doesn't work as expected because ret created by the base Breeze.WebApi.BreezeConfig.CreateJsonSerializerSettings() adds IsoDateTimeConverter to the ret.Converters list. By default IsoDateTimeConverter will not add 'Z' to the output string if DateTime.Kind is DateTimeKind.Unspecified. Like Jay Trabant answered -
breeze.js fixes this client-side.
With this knowledge it's ease to make your server return all dates as UTC:
var isoDateTimeConverter = ret.Converters.OfType<Newtonsoft.Json.Converters.IsoDateTimeConverter>().Single();
isoDateTimeConverter.DateTimeStyles = System.Globalization.DateTimeStyles.AssumeUniversal;
Because there is a bug in IsoDateTimeConverter - Issue with DateTimeStyles, it's better to completely remove IsoDateTimeConverter from the Converters collection and set DateTimeZoneHandling to Utc:
public class CustomBreezeConfig : Breeze.ContextProvider.BreezeConfig
{
protected override Newtonsoft.Json.JsonSerializerSettings CreateJsonSerializerSettings()
{
var settings = base.CreateJsonSerializerSettings();
settings.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Utc;
var isoDateTimeConverter = settings.Converters.OfType<Newtonsoft.Json.Converters.IsoDateTimeConverter>().Single();
settings.Converters.Remove(isoDateTimeConverter);
return settings;
}
}
My guess is that your server datatype is a DateTime ( not a DateTime2 or DateTimeOffset), and as such has no explicit timezone information.
Breeze does not manipulate the datetimes going to and from the server in any way EXCEPT to add a UTZ timezone specifier to any dates returned from the server that do not already have one. This is only done because different browsers interpret dates without a timezone specifier differently and we want consistency between browsers.
This is discussed in more detail in the answer posted here. breezejs: date is not set to the right time

Resources