I am trying to add ticks to a DateTime (c#), but I can't figure out why is this not working:
DateTime end = StartTime.AddTicks(timer.Interval);
Console.WriteLine(StartTime.ToLocalTime() + " + " + timer.Interval + " = " + end.ToLocalTime());
This is giving me the following output :
24/05/2014 20:47:27 + 60000 = 24/05/2014 20:47:27
So, why is end the same value than StartTime ?
FYI, I declared my variables like this :
private DateTime StartTime;
private Timer timer = new Timer();
10.000 ticks make 1ms, 60.000 ticks make 6ms, you don't display millisecond part so there is no difference in output, but values are different.
If you change your output formatting to the following (display millisecond part), then you can see the difference.
Console.WriteLine(StartTime.ToLocalTime().ToString("dd/MM/yyyy HH:mm:ss.fff") + " + " + timer.Interval + " = " + end.ToLocalTime().ToString("dd/MM/yyyy HH:mm:ss.fff"));
More about ticks: DateTime.Ticks
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 have been trying to make a simple second to hour and minute converter just to practice some C#. The weird thing is as I pass 599 seconds mark, the program subtracts 60 seconds. So, 540 seconds equals 9 minutes; 599 seconds equals 9 minutes 59 seconds, but 600 seconds equals to 9 minutes. I tried using a button to trigger the commands instead of textchanged and button did the job fine. So, I reckon, the root of the problem must be textchanged event itself. I'll be adding three examples. Screenshot images are in Turkish but you will easily get the idea.
How can I solve this, and what causes this problem? This is really mind boggling.
Screenshot
int second, minute, hour, minuteLeft, secondLeft;
private void txtTime_TextChanged(object sender, EventArgs e)
{
CalculateTime();
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
void CalculateTime()
{
if (txtTime.Text != "")
{
second = Convert.ToInt32(txtSure.Text);
secondLeft = second % 60;
second = second - minuteLeft;
minute = second / 60;
minuteLeft = minute % 60;
minute = minute - minuteLeft;
hour = minute / 60;
lblMsg.Text = hour.ToString() + " hours, " + minuteLeft.ToString() + " minutes " + secondLeft.ToString() + " seconds.";
}
else
{
lblMsg.Text = "";
}
You are subtracting minuteLeft from the number of seconds:
second = second - minuteLeft;
That should be subtracting the secondLeft value:
second = second - secondLeft;
You could consider attacking the problem in a different way:
var durationInSeconds = Convert.ToInt32(txtTime.Text);
var duration = new TimeSpan(0, 0, durationInSeconds);
var second = duration.Seconds;
var minute = duration.Minutes;
var hour = Convert.ToInt32(Math.Truncate(duration.TotalHours));
TimeSpan is well suited to this class of problem, and will allow you to 'automatically' extract the hour, minute and second component of the duration.
You could also consider replacing:
hour.ToString() + " hours, " + minuteLeft.ToString() + " minutes " + secondLeft.ToString() + " seconds.";
with a call to:
public static string ToPrettyFormat(TimeSpan timeSpan)
{
var dayParts = new[] { GetDays(timeSpan), GetHours(timeSpan), GetMinutes(timeSpan) }
.Where(s => !string.IsNullOrEmpty(s))
.ToArray();
var numberOfParts = dayParts.Length;
string result;
if (numberOfParts < 2)
result = dayParts.FirstOrDefault() ?? string.Empty;
else
result = string.Join(", ", dayParts, 0, numberOfParts - 1) + " and " + dayParts[numberOfParts - 1];
return result.UppercaseFirst();
}
Stolen from https://codereview.stackexchange.com/questions/24995/convert-timespan-to-readable-text .
I am currently designing a ticket system for my place of work.
Currently, I have most of it working except one small part. It is setup to automatically send it to the person that is responsible for fixing it. After it tells the user their Unique ID and the estimated date it should be finished. The only problem is People work different shifts. The things I want it to be able to do is
If a ticket is put in before the persons shift, it adjusts to the time they come in for their shift.
If a ticket is put in after a persons shift, adjusts it to the next day and the start of the persons shift.
If the ticket takes longer than the time remaining in the shift, use the time remaining in the shift and the remaining in the following day.
Be able to add time from previous tickets that are not completed and maintain the work schedule. (Currently working except maintaining work schedule)
For example:
First shift comes in at 7:00 am and someone puts in a ticket at 3:00 am, it should go to 7:00 am and than adjust the amount of time needed.
First shift leaves at 3:30pm and someone puts in a ticket at 3:00 pm that takes roughly an hour to complete, it should finish the 30 minutes in the day and go 30 minutes into the following day.
The code I currently have to try to handle this
if (DateTime.Now.AddMinutes(sum).Hour <= 8)
{
MessageBox.Show("Too early");
var now = DateTime.Now;
var tomorrow8am = now.AddDays(0).Date.AddHours(8);
double totalHours = (tomorrow8am - now).TotalHours;
MessageBox.Show("totalHours=" + totalHours);
var today8am = now.Date.AddHours(8).Hour;
EstimatedCompleteDate = DateTime.Today.Month + "/" + DateTime.Today.AddDays(0).Day + "/" + DateTime.Today.Year + " " + DateTime.Now.AddHours(totalHours).AddMinutes(timetoadd).ToLongTimeString();
MessageBox.Show(now.ToString() + Environment.NewLine + tomorrow8am.ToString() + Environment.NewLine + totalHours.ToString());
MessageBox.Show(now.AddHours(totalHours).AddMinutes(timetoadd).Hour.ToString());
if (DateTime.Now.AddHours(totalHours).AddMinutes(timetoadd).Hour > 15)
{
double hourstoadd = 0;// = timetoadd / 60;
do
{
hourstoadd++;
timetoadd = timetoadd - 60;
}
while (timetoadd > 60);
var remaining = hourstoadd;
int i = 0;
MessageBox.Show(remaining.ToString());
do
{
if (remaining > 0)
{
MessageBox.Show("Remaining1: " + remaining.ToString());
MessageBox.Show("Remaining - totalHours" + (remaining - totalHours).ToString());
i++;
EstimatedCompleteDate = DateTime.Today.Month + "/" + DateTime.Today.AddDays(i).Day + "/" + DateTime.Today.Year + " " + DateTime.Now.AddHours(totalHours + remaining).AddMinutes(timetoadd + now.Minute).ToLongTimeString();
remaining = remaining - 8;
}
}
while (remaining > 0);
please excuse the messagebox's I am using to give some feedback to me.
My code parses a raw data and turns it into a datetime string format such as below
string birthday = "20" + year + "-" + month + "-" + day + " " + hour + ":" + minutes + ":" + second;
So birthday is a string and then I converted it to datetime format such as below
DateTime bdaylater= Convert.ToDateTime(birthday);
The result became like the one below
7/8/2015 5:02:05 AM
in which I wanted to convert to something like the one below
2015/07/08 05:02:05.000
How can I achieve such result.
Thanks.
You should format your date variable before printing the same
string birthday = "2015" + "-" + "07" + "-" + "08" + " " + "05" + ":" + "02" + ":" + "05";
DateTime bdaylater= Convert.ToDateTime(birthday);
Console.WriteLine(bdaylater.ToString("yyyy/MM/dd hh:mm:ss.mmm"));
this will give the o/p 2015/07/08 05:02:05.02
Fiddle example
If you want only 000 in millisecond the string will be "yyyy/MM/dd hh:mm:ss.000"
Would you like to convert it to string format? With 3 zeros padded on it? Maybe this could help:
int year = 15;
int month = 7;
int day = 8;
int hour = 5;
int minutes = 2;
int second = 5;
string birthday = "20" + year + "-" + month + "-" + day + " " + hour + ":" + minutes + ":" + second;
DateTime bdaylater = Convert.ToDateTime(birthday);
Console.WriteLine(bdaylater);
Console.WriteLine(bdaylater.ToString("yyyy/MM/dd hh:mm:ss.000")); //2015/07/08 05:02:05.000
Console.ReadLine();
Try this:
DateTime.Parse(birthday).ToString("yyyy/MM/dd hh:mm:ss.fff")
I want to subtract two Datetime values. I collected them separately from the form and concatenate it in C#. The result shows in Hour format in textbox like "23:00".
Here is what I tried:
string start = StartDateTxtBox.Text+" "+StartHourDrop.Text+":"+StartMinuteDrop.Text+":00";
string End = EndDateTxtBox.Text + " " + EndHourDrop.Text + ":" + EndMinuteDrop.Text + ":00";
DateTime allstart = DateTime.Parse(start);
DateTime allEnd = DateTime.Parse(End);
Int32 MinuteDiff = Convert.ToInt32(allEnd.Subtract(allstart).TotalMinutes);
Int32 Hour = MinuteDiff / 60 / 24;
DateTime conversiontotime = System.Convert.ToDateTime(Hour);
LastID.Text = conversiontotime.ToString();
But it doesn`t work.
Is there another (easy) way to do this, or can anybody tell me what the problem in my code is?
DateTime allstart = DateTime.Parse("03/04/2015 09:15");
DateTime allEnd = DateTime.Parse("03/04/2015 09:37");
TimeSpan tsdiff = allEnd - allstart;
LastID.Text = tsdiff.ToString();