What I tried is:
select cast(sum(datediff(second,0,totalhr))/3600 as varchar(12)) + ':' +
right('0' + cast(sum(datediff(second,0,totalhr))/60%60 as varchar(2)),2) +
':' + right('0' + cast(sum(datediff(second,0,totalhr))%60 as varchar(2)),2) as total
FROM checkinout where YEAR(date)=2019 and MONTH(date)=09 and userid=5
It giving wrong output 112:53:04 the right answer should be 116:30:04
If the totalhr column type is time and the format is hh:mm:ss,
For SQL Server: DATEADD (datepart , number , date )
SELECT DATEADD (ms, SUM (DATEDIFF (ms, '00:00:00.000', totalhr)), '00:00:00.000') AS total
FROM checkinout
WHERE
YEAR(date) = 2019 AND
MONTH(date) = 9 AND
userid = 5;
This function adds a specified number value (as a signed integer) to a specified datepart of an input date value, and then returns that modified value.
the above condition will return the calculated Date and Time but if you want just the hh:mm:ss in two digit format you can use:
SELECT FORMAT(hrs, '0#') + ':' + FORMAT(mins, '0#') + ':' + FORMAT(secs, '0#') as total FROM
(SELECT hrs + (((((mins * 60) + (secs - (secs % 60))) / 60) - ((((mins * 60) + (secs - (secs % 60))) / 60) % 60)) / 60) AS hrs,
(((mins * 60) + (secs - (secs % 60))) / 60) % 60 AS mins,
secs % 60 AS secs
FROM (
SELECT SUM(116) AS hrs, // you have to replace the number with your column
SUM(30) AS mins, // you have to replace the number with your column
SUM(04) AS secs // you have to replace the number with your column
) AS dateSplit) AS total
try running below query
SELECT cast(sum(DATEPART(HOUR,totalhr)) as varchar(max)) + ':' +
cast(sum(DATEPART(MINUTE,totalhr)) as varchar(max)) + ':' +
cast(sum(DATEPART(SECOND,totalhr)) as varchar(max)) FROM checkinout
Related
const string query = "SELECT worker, date_trunc('hour', created) AS created, AVG(hashrate) AS hashrate, " + "AVG(sharespersecond) AS sharespersecond FROM minerstats " +
"WHERE poolid = #poolId AND miner = #miner AND created >= #start AND created <= #end " +
"GROUP BY date_trunc('hour', created), worker " +
"ORDER BY created, worker;";
This query displays the time periods of hours.
how to make 15 minutes?
Here is one option:
date_trunc('hour', created)
+ extract(minute from created)::int / 15 * interval '15' minute
I am using this linq query,it works fine. only problem is it is returning hours in int i.e 10,9 etc but I want complete hours along with minutes i.e 10:28,9:45 etc
DayHours = (from ab in db.Attendances
where ab.Employee == 63
&& ab.InTime.Value.Year == 2015
&& ab.InTime.Value.Month == i
select new
{
Day = ab.InTime.Value.Day,
Hours = DbFunctions.DiffMinutes(ab.InTime, ab.OutTime) / 60
});
You can try this:
/*...*/
select new
{
Day = ab.InTime.Value.Day,
Hours = DbFunctions.DiffMinutes(ab.InTime, ab.OutTime) / 60
Minutes = DbFunctions.DiffMinutes(ab.InTime, ab.OutTime) % 60
});
If you don't want it on this format, please specify exactly how do you want to store it (a Timespan variable? something else?)
You Can get Exact Time from DB as
select round(
Cast(
DateDiff(
MINUTE,
convert(varchar(5), '09:40:00.0000000', 108),
convert(varchar(5), '18:12:00.0000000', 108)
) / 60 as float
)
+ Cast(
DateDiff(
MINUTE,
convert(varchar(5), '09:40:00.0000000', 108),
convert(varchar(5), '18:12:00.0000000', 108)
) % 60 as float
) / 60, 2
) as [Hour in office]
I want all people that there age is 27
how i calc the start and the end date?
ss.startAge = (DateTime.Now.Year - startAge).ToString() +
"-" + DateTime.Now.ToString("MM") + "-" + DateTime.Now.ToString("dd");
ss.endAge = (DateTime.Now.Year - (endAge-1)).ToString() +
"-" + DateTime.Now.ToString("MM") + "-" + DateTime.Now.ToString("dd");
i know that i need to sub 1 day but if the end age is need to be
1999-12-31 because the calc is above is give 2000-01-01 how i need to
write the code that the end age will be correct
how i calc the end date correctly?
I need to convert fast the string in format "HHmmss" to DateTime or integers. I've tested such code:
Console.WriteLine("decoding " + text);
long microseconds = sw.ElapsedTicks / (Stopwatch.Frequency / (1000L * 1000L));
Console.WriteLine("start time " + microseconds);
field = DateTime.ParseExact(text, "HHmmss", null);
microseconds = sw.ElapsedTicks / (Stopwatch.Frequency / (1000L * 1000L));
Console.WriteLine("finish time " + microseconds);
and the output is
decoding 172400
start time 121
finish time 244
decoding 172400
start time 236
finish time 383
decoding 172400
start time 116
finish time 416
decoding 172400
start time 235
finish time 421
decoding 172359
start time 149
finish time 323
so in average about 150 microseconds. What's a lot of time, i'm writing HFT software and the best HFT has in average 10 microseconds "tick-to-trade" time (this includes everything!). I understand that using c# this is imposible however i still think that 150 microseconds is too much even using c#.
Now I want to use another algorithm, however I don't know how to "extract" integers from the text:
field = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, /*extract hour, min, sec from text*/)
What can you suggest and what would be the fastest way?
Please do not ask why I'm care about perfomance instead just suggest how to do that faster.
Results:
Using DateTime.ParseExact(text, "HHmmss", null) about 6-8 ticks
Using TimeSpan ts = TimeSpan.ParseExact(text, "hhmmss", null); about 3-4 ticks
Using int hour = 10 * text[0] + text[1] - 11 * '0';... about 0 ticks
Acutally much less than 0 ticks if using loop for measurements. Actually it was found that last version is 100 times faster than other.
Code:
long startMicroseconds = sw.ElapsedTicks /*/ (Stopwatch.Frequency / (1000L * 1000L))*/;
//TimeSpan ts = TimeSpan.ParseExact(text, "hhmmss", null);
//int hour = 10 * text[0] + text[1] - 11 * '0';
//int minute = 10 * text[2] + text[3] - 11 * '0';
//int second = 10 * text[4] + text[5] - 11 * '0';
field = DateTime.ParseExact(text, "HHmmss", null);
long finishMicroseconds = sw.ElapsedTicks /*/ (Stopwatch.Frequency / (1000L * 1000L))*/;
Console.WriteLine("elappsed " + (finishMicroseconds - startMicroseconds));
This approach doesn't use any string substring or parsing methods. It uses only indexing and simple arithmetic:
int hour = (s[0] - '0') * 10 + s[1] - '0';
int minute = (s[2] - '0') * 10 + s[3] - '0';
int second = (s[4] - '0') * 10 + s[5] - '0';
This next version is probably even faster because the calculation has been partially evaulated to help the compiler. As a result it is slightly harder to read and understand:
int hour = s[0] * 10 + s[1] - '0' * 11;
int minute = s[2] * 10 + s[3] - '0' * 11;
int second = s[4] * 10 + s[5] - '0' * 11;
For kicks you might also want to see if this is even faster, though I suspect that this code will be the same as the previous version:
int hour = s[0] * 10 + s[1] - 528;
int minute = s[2] * 10 + s[3] - 528;
int second = s[4] * 10 + s[5] - 528;
If you really want performance instead of readability you can work with raw chars directly:
hour = 10*s[0] + s[1] - 11*'0';
minute = 10*s[2] + s[3] - 11*'0';
second = 10*s[4] + s[5] - 11*'0';
btw. DateTime.Now is quite slow because it needs to convert the current time to the local time-zone. You should use DateTime.UtcNow instead. On my comp DateTime.UtcNow costs 9ns, DateTime.Now costs 900ns.
You also should fetch DateTime.UtcNow only once, else you get a race-condition.
Is this really too slow?
TimeSpan ts = TimeSpan.ParseExact("172406", "hhmmss", null);
int hh = ts.Hours;
int mm = ts.Minutes;
int ss = ts.Seconds;
It is at least easy to understand.
I am using asp.net 3.5 with C#.
I want to create a countdown timer and my requirement is like this:
Countdown end date: June 16 2010
So, till June 16 comes my timer will show the remeaning time.
Please let me know how to achieve it, I google it but i didn't get the excat solution to my problem.
Thanks in advance.
This is something you need to solve with Javascript. The only thing you need to do from the server is set the end date as a Javascript variable. You need Javascript because you only load the page from the server. Afterwards you need to handle the countdown on the client.
Javascript
<script type="text/javascript">
function countdown_clock(clockID, year, month, day, hour, minute) {
countdown(clockID, year, month, day, hour, minute);
}
function countdown(clockID, year, month, day, hour, minute) {
Today = new Date();
Todays_Year = Today.getFullYear();
Todays_Month = Today.getMonth();
//Convert both today's date and the target date into miliseconds.
Todays_Date = (new Date(Todays_Year, Todays_Month, Today.getDate(),
Today.getHours(), Today.getMinutes(), Today.getSeconds())).getTime();
Target_Date = (new Date(year, month - 1, day, hour, minute, 00)).getTime();
//Find their difference, and convert that into seconds.
Time_Left = Math.round((Target_Date - Todays_Date) / 1000);
if (Time_Left < 0)
Time_Left = 0;
days = Math.floor(Time_Left / (60 * 60 * 24));
Time_Left %= (60 * 60 * 24);
hours = Math.floor(Time_Left / (60 * 60));
Time_Left %= (60 * 60);
minutes = Math.floor(Time_Left / 60);
Time_Left %= 60;
seconds = Time_Left;
dps = 's'; hps = 's'; mps = 's'; sps = 's';
//ps is short for plural suffix.
if (days == 1) dps = '';
if (hours == 1) hps = '';
if (minutes == 1) mps = '';
if (seconds == 1) sps = '';
var clock = document.getElementById(clockID);
clock.innerHTML = days + ' day' + dps + ' ';
clock.innerHTML += hours + ' hour' + hps + ' ';
clock.innerHTML += minutes + ' minute' + mps + ' and ';
clock.innerHTML += seconds + ' second' + sps;
//Recursive call, keeps the clock ticking.
setTimeout('countdown("' + clockID + '",' + year + ',' + month + ',' + day + ',' + hour + ',' + minute + ');', 1000);
}
</script>
ASP.NET
protected override void OnPreRender(EventArgs e)
{
DateTime endDate = new DateTime(2010, 6, 1, 0, 0, 0);
string script = string.Format("countdown_clock('clock', {0}, {1}, {2}, {3}, {4});", endDate.Year, endDate.Month, endDate.Day, endDate.Hour, endDate.Minute);
ScriptManager.RegisterStartupScript(this, this.GetType(), "countdown", script, true);
base.OnPreRender(e);
}
Script taken an modified for example purpose from My Little Scripts.
If you like it easy, use DateTime.
DateTime EventTime = new DateTime(2010, 6, 16);
TimeSpan Duration = EventTime - DateTime.Now;
string TimeTillEvent = Duration.ToString();