Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I created a count down timer like this:
I set manuel values for startTime
var options = {
stepTime: 60,
format: "dd:hh:mm:ss",
startTime: "01:12:32:55",
digitImages: 6,
digitWidth: 53,
digitHeight: 77,
image: "digits.png"
};
I have lblDay,lblHours,lblMinutes,LblSeconds on page.
I can get TotalHours column from SQL.How can I convert value of TotalHours to like this :
lbDays:27
lblHours:2
lblMinutes:34
lblSeconds:08
int TotalHours = Convert.ToInt32(dt.Rows[0][11]);
//int TotalHours =664
and How can I set value of labels to startTime
int totalHours = 664; // example from question
TimeSpan ts = TimeSpan.FromHours(totalHours); // or similar
int days = (int)ts.TotalDays,
hours = ts.Hours,
// note the next will always be zero
// since we init in an integer
// number of hours
minutes = ts.Minutes,
seconds = ts.Seconds;
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 months ago.
Improve this question
I have a period suppose its Start from 01/12/2022 and end in 30/12/2022 I want to divide this period to weeks the week must start from Saturday and end in Friday so expected result should be :
How I can do this using SQL server query
After a little bit of trial and error you need this:
DateTime StartOfWeek(DateTime when) =>
when.AddDays(-((8 + (int)when.DayOfWeek) % 7));
Here's a table of output:
var table =
Enumerable
.Range(0, 31)
.Select(x => new DateTime(2022, 12, 1).AddDays(x))
.GroupBy(x => StartOfWeek(x))
.Select(x => new
{
from = x.Min(),
to = x.Max(),
start = x.Key
});
A similar solution to Enigmativity's, for those who are not yet familiar with queries.
In general, you can find out the first day of the week like this:
(and choose which day the week should start)
DateTime curDate = new DateTime(2022, 12, 1);
DateTime result = FirstDateOfWeek(curDate, DayOfWeek.Saturday);
DateTime FirstDateOfWeek(DateTime day, DayOfWeek firstDayOfWeek=DayOfWeek.Monday)
{
int minus = day.DayOfWeek - firstDayOfWeek;
if (minus == 0)
return day.Date;
if (minus < 0)
minus = 7 + minus;
return day.Date - TimeSpan.FromDays(minus);
}
Just add 7 to the result for the next week.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Hello I am quite new to C# and I am facing difficulty in adding time as an int . For example,
01:30 Hr
09:45 Hr
13:27 Hr
are hours. I want to convert these to minutes.
How do I add these as int so that I get:
90 + 585 + 825
Is there a way to perform this operation in C#?
public int total_time;
void Start()
{
total_time = time_1 + time_2 + time_3;
}
Let's suppose that your time data are stored in this way:
var dataSource = new List<string>
{
"01:30 Hr",
"09:45 Hr",
"13:27 Hr"
};
Then all you need to do is to
get ride of the Hr suffix
convert them to TimeSpan
sum their TotalMinutes
var total = dataSource
.Select(data => data.Substring(0, 5))
.Select(time => TimeSpan.Parse(time).TotalMinutes)
.Sum();
Obviously this is a naive implementation and really fragile.
A more robust solution would use
regex to get the time portion of the string
use TimeSpan's TryParse to avoid runtime exceptions in case of mailformed inputs.
UPDATE
Based on the comment of Drag and Drop, here is an alternative which could give you better precision:
var total = dataSource
.Select(data => data.Substring(0, 5))
.Select(time => TimeSpan.Parse(time))
.Aggregate(TimeSpan.Zero, (t1, t2) => t1 + t2)
.TotalMinutes;
This approach could be used as well whenever you introduce seconds as well.
(You just need to adjust the Substring parameters.)
Why don't you use TimeSpan struct? TimeSpan struct does exactly what you want.
TimeSpan thirtyMinute = new TimeSpan(0, 30, 0); // Hours, Minutes, Seconds
TimeSpan s = new TimeSpan(1, 24, 60, 60, 100); // Day, Hours, Minutes, Seconds, Miliseconds
TimeSpan oneHour = thirtyMinute + thirtyMinute; // You can add TimeSpan to another == 1 Hour
Here is how to convert hour string
Debug.Log(string.Format("You were out of game for: {0} minutes and {1:00} seconds", (int)totalExitTime / 60, (int)totalExitTime % 60));
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I'm currently working on a hotel management system's invoice (asp.net c#)... My question is that how to show the round off on a label example if the total amount is 8023.25 round off label should show .25 and totallabel should show 8023...
Please can anybody help me with this {with code} ???
You can do it mathematically:
Get the integral part by casting it with int.
To get the decimal portion, deduct the total amount with the intergral portion:
double amt = 8023.25;
int value = (int)amt; //value will become 8023
double fraction = amt - value; //fractionwill become 0.25
You can split it and use them like:
string s = inputValue.ToString("8023.25", CultureInfo.InvariantCulture);
string[] parts = s.Split('.');
int i1 = int.Parse(parts[0]);
int i2 = int.Parse(parts[1]);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Is it possible to do it on this format?
public static int[,] Values
I'm feeling generous, try this, but note that in your question your array dimensions were backwards IMHO ...
// Inputs
int year = 2016;
int month = 9;
int numberOfWeeksToList = 5;
// Create a datetime object for the first day of the month you're interested in
var firstOfMonth = new DateTime(year, month, 1);
// Get the day of the week for that date
var dowForFirstOfMonth = (int) firstOfMonth.DayOfWeek;
// You want to start the week on a Monday, so get the difference between the days
// -- note the DayOfWeek property has 0 for Sunday, so we add a week and mod to handle
// negative differences
var numberOfDaysFromPreviousMonthToInclude = (dowForFirstOfMonth - (int) DayOfWeek.Monday + 7)%7;
// The first date to include is the first date of the month subtract the number of days
// from the previous month to include, i.e. stepping back in time a few days
var firstDateToList = firstOfMonth.AddDays(-numberOfDaysFromPreviousMonthToInclude);
// Create a 2D array, with the minor dimension representing one week
int[,] daysInMonthByWeek = new int[numberOfWeeksToList, 7];
// Now loop through each week's worth of dates, up to the configured number of weeks ...
int dateIndex = 0;
for (int weekIndex = 0; weekIndex < numberOfWeeksToList; ++weekIndex)
{
for (int dayIndex = 0; dayIndex < 7; ++dayIndex)
{
// Add the date index to the previously-computed first date to be displayed, and
// include the 'Day' component of the date
daysInMonthByWeek[weekIndex, dayIndex] = firstDateToList.AddDays(dateIndex).Day;
++dateIndex;
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Is there a simpler way to calculate this? Perhaps via a method built-in to .NET?
var time = DateTime.Now.AddSeconds(8);
var year = 10000000000000 * (ulong)(time.Year - 1900);
var month = 100000000000 * (ulong)(time.Month - 1);
var dayofyear = 100000000 * (ulong)(time.DayOfYear - 1);
var day = (ulong)(time.Day * 1000000);
var Hour = (ulong)(time.Hour * 10000);
var Minute = (ulong)(time.Minute * 100);
var Second = (ulong)(time.Second);
var utcTime = year + month + dayofyear + day + Hour + Minute + Second;
Could it be DateTime.Now.AddSeconds(8).AllMilliseconds?
I've tried these methods
var test = (time - DateTime.MinValue).Milliseconds;
var tes2t = (time - DateTime.MinValue).TotalMilliseconds;
DateTime.MinValue is not 1900. The documentation for DateTime.MinValue says:
The value of this constant is equivalent to 00:00:00.0000000 UTC, January 1, 0001, in the Gregorian calendar.
Which is about 1899 years before the date you want. You want to do this:
double milliseconds = ( time - new DateTime( 1900, 1, 1 ) ).TotalMilliseconds
EDIT: As Rick suggests in the comments, the following might be better suited to your needs:
var epoch = new DateTime( 1900, 1, 1, 0, 0, 0, DateTimeKind.Utc );
double milliseconds = ( time - epoch ).TotalMilliseconds;