Is there an easy way to present time (hh:mm) as a decimal value?
Example, 01:08 should become 1,13.
I have an asp:textbox masked (ajax) as time with the mask format "99:99". Next to this box I need to present the entered value as decimal.
<asp:TextBox ID="time" runat="server" /> hh:mm ([time in decimal format])
<ajaxToolkit:MaskedEditExtender runat="server" Mask="99:99" TargetControlID="time" MaskType="Time" />
You can use the TotalHours property of the TimeSpan, like this:
DateTime endHour = new DateTime(2010, 1, 2, 5, 30, 0);
double totalHours = new TimeSpan(endHour.Hour, endHour.Minute, 0).TotalHours;
First up, you might find it more appropriate to use a TimeSpan rather than a DateTime. So for your example, this:
TimeSpan t = new TimeSpan(0, 1, 8);
decimal d = t.Minutes + (t.Seconds / 60m);
Console.WriteLine(d.ToString());
produces the correct result. The m after the 60 forces that calculation to be decimal.
You can't hook this C# directly to the onblur of the textbox, as it runs server side. If you need this to run client side you will have to either use an ajax callback to evaluate it, or use javascript to cut the string up, then calculate each bit (minutes and seconds) individually, then output the result to the second textbox or label.
double decimalTime = DateTime.Now.Hour + (double)DateTime.Now.Minute/60
Edit: Better way, using GenEric's TimeSpan idea:
double hours = new TimeSpan(DateTime.Now.Hour,
DateTime.Now.Minute,
0).TotalHours;
(CDate("01:08").TimeOfDay.TotalHours).ToString("N2"))
Related
I am looking to find the difference in days of two ajax calendar extenders that are linked to two separate text boxes.
Dim dt1 As DateTime = Convert.ToDateTime(CalendarExtender1.SelectedDate)
Dim dt2 As DateTime = Convert.ToDateTime(CalendarExtender2.SelectedDate)
Dim diffInDays As Integer = dt2.Subtract(dt1).Days
Label12.text = "The dates are " + diffInDays.ToString() + "days appart."
I have completed this task using the asp calendar control previously but I am having issues when trying to achieve this using the ajax calendar extension.
the label output is stating " the two dates are 0 days apart " for numerous tested dates.
I would appreciate if anyone could give any guidance/help or let me know where i am going wrong.
Thanks!
Use the TimeSpan object :)
Hacky example:
Sub Main()
Console.WriteLine(getDayDifference(CDate("2017-06-03"), CDate("2017-06-03"), True).ToString)
Console.WriteLine(getDayDifference(CDate("2017-06-03"), CDate("2017-05-03"), True).ToString)
Console.WriteLine(getDayDifference(CDate("2017-02-03"), CDate("2017-05-03"), True).ToString)
Console.ReadKey()
End Sub
Public Function getDayDifference(first As DateTime, second As DateTime, alwaysPositive As Boolean)
Dim span As TimeSpan = second - first
Return If(alwaysPositive, Math.Abs(span.TotalDays), span.TotalDays)
End Function
Will return:
0
31
89
You can set the alwaysPositive arg to False if you want the true polarity (helpful when determining if the first date is lower than the second), eg:
0
-31
89
Can anyone lead me in the right direction as to how to set a textbox such as this:
<td><asp:TextBox ID="txtBoxDate" runat="server" CssClass="fields"></asp:TextBox></td>
Into a masked text box that meets this style "mm/dd/yyyy"
Why not just simply convert the DateTime object to that format in server-side code?
Try this
DateTime dateTime = "your-date-time";
var asString = dateTime.ToString("mm/dd/yyyy");
asString will now have the format you've converted the DateTime object to. It will be string. So, after this step you can execute this
txtBoxDate.Text = asString;
..this might be the code you're looking for.
I am using Linq to entityframework to query some infomration. I am trying to use entityfunction.truncatetime and it doesnt seem to work as expected. here is my sample query
From d In Request
Where d.Requestor= "XXXX" And d.ProcessedFlag = "N"
Select d.RequestID, RequestReason = d.RequestReason.ItemValue, RequestType = d.RequestType.ItemValue, RequestedDate = EntityFunctions.TruncateTime(d.RequestedMoveDate)
The requesteddate doesnt seem to truncate the time part and I am still getting the both Date and time.
Am I missing something here?
In .NET, the DateTime class actually represents both a date and a time. Internally, this is stored as a numeric value represented by the number of 100-nanosecond "ticks" since Midnight, January 1, 1001 AD. This number gets "converted" when it's displayed (either in output or in a debugger). This conversion is done via a format string.
Even if you truncate a DateTime's time portion, it still has a time... it's just 00:00:00, and if you don't want to see that time, you need to adjust your format string to not convert that.
Thus, if you do something like this: DateTime.Now.Date it will display `10/15/2012 00:00:00" if you use the default date conversion string (or whatever is the default format for your culture).
If you want to only display the Date portion, then you must do something like myDate.ToShortDateString() or myDate.ToString("d").
EntityFunctions is a set of tools designed to be used in Linq to Entities queries, because doing DateTime formatting is not normally allowed in a query.
For example, this code does not work:
var q = from x in dc where x.BirthDate == DateTime.Now.AddYears(-15).Date select x;
You have to do it like this:
var q = from x in dc
where x.Birthdate == EntityFunctions.TruncateTime(DateTime.Now.AddYears(-15))
select x;
This will then generate the correct SQL to do date comparisons in SQL code. This is what the EntityFunctions are designed for, not truncating dates in the select portion (although it does work). But, even though the date is truncated, it will still have a Time component, it will just be 00:00:00, and you must use a date format string to present it to your users in the manner you intend.
cant you use ToShortDateString() like below?
List<DateTime> time = new List<DateTime>();
time.Add(DateTime.Now);
var WhatDate = from date in time
select new { Date = date.ToShortDateString() };
In your case try this
From d In Request
Where d.Requestor= "XXXX" And d.ProcessedFlag = "N"
Select new{ RequestID = d.RequestID, RequestReason = d.RequestReason.ItemValue, RequestType = d.RequestType.ItemValue, RequestedDate = d.RequestedMoveDate.ToShortDateString()};
I really neeed dd-mon-yy format because my oracle database accepts dates in this format.
Can I validate like that in RegularExpressionValidator like that?
And also, do I have to convert the textbox value to oracle data time format, when using nhibernate?
thanks a lot for help;
try
Regx.ValidationExpression=#"^(([1-9])|(0[1-9])|(1[0-2]))\-((0[1-9])
|([1-31]))\-((\d{2})|(\d{4}))$";
or
you can try this custom validator instead
protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
{
DateTime result;
if (!DateTime.TryParse(args.Value, out result))
{
args.IsValid = false;
return;
} else args.IsValid = true;
}
Is it necessary to force the user to enter the data in the format proscribed by the Oracle database? As long as the user enters a valid date, you can parse the input into a DateTime object and then call ToString("dd-MMMM-yy") to produce a representation of the date in the format required by the database. This gives you the freedom to provide more a user-friendly means of collecting input from the user e.g. a date/time picker type control.
For two digits for the year try:
^([012]?\d|3[01])-([Jj][Aa][Nn]|[Ff][Ee][Bb]|[Mm][Aa][Rr]|[Aa][Pp][Rr]|[Mm][Aa][Yy]|[Jj][Uu][Nn]|[Jj][Uu][Ll]|[Aa][Uu][Gg]|[Ss][Ee][Pp]|[Oo][Cc][Tt]|[Nn][Oo][Vv]|[Dd][Ee][Cc])-\d\d$
With four digits for the year:
^([012]?\d|3[01])-([Jj][Aa][Nn]|[Ff][Ee][Bb]|[Mm][Aa][Rr]|[Aa][Pp][Rr]|[Mm][Aa][Yy]|[Jj][Uu][Nn]|[Jj][Uu][Ll]|[Aa][Uu][Gg]|[Ss][Ee][Pp]|[Oo][Cc][Tt]|[Nn][Oo][Vv]|[Dd][Ee][Cc])-(19|20)\d\d$
Those expressions mean:
0 or 1 or 2 followed by a number, or 3 plus 0 or 1
next a dash
For the month it 'tries' all the combinations of JAN, JAn, Jan, jan, etc... for all the twelve months
next a dash
Finally it expects two digits for 'yyyy' version or 19 or 20 followed by two digits for 'yy' version
I have 2 textbox in my asp.net page and also have one hiddenfield in my asp.net page , my hiddenfield will always have numeric value like 123.00 , and in my one textbox also I will always have numeric value like 20.00 now I want to add this hiddenfield value and textbox value and display it into second textbox thru javascript
I wrote the following code to do this
var amt = document.getElementById("txtsecond");
var hiddenamt = document.getElementById("AmtHidden").value
var fee = document.getElementById("txtFirst").value;
amt.value = hiddenamt + fee;
this should give me result like 123.00+20.00 = 143.00 but this is concatnating hiddenamt value and fee value and giving me result like 12320.00 in my first textbox
can anybody suggest me what is wrong in my code and what is the right way to get desired value
amt.value = parseFloat(hiddenamt) + parseFloat(fee);
the value of an input is just a string - convert to float parseFloat(foo) in JS and you'll be fine
edited to make float as I notice it's probably important for you
Textboxes are strings, you need to convert from a String to a Number:
var hiddenamt = parseFloat(document.getElementById("AmtHidden").value);
var fee = parseFloat(document.getElementById("txtFirst").value);
Eric
you should parse the values to decimals first:
decimal amt, hiddenamt, fee;
Decimal.TryParse(document.getElementById("txtsecond"),out amt);
Decimal.TryParse(document.getElementById("txtfirst"),out fee);
hiddenamt = amt + fee;