RIAS
asked on
Create a datatable with rows
How to create a datatable with the time format string incremeted with nearest 15 minutes.
I am using vb.net
example:
Dim dt as new datatable
If the string value is 2:32 then the datatable should have rows in time incrementing by around 15 minutes :
Having rows like :
2:45
3:00
3:15
example2:
String value is 4:34 then the datatable should have rows in time incrementing by around 15 minutes :
Having rows like :
4:45
5:00
5:15
Please let me know if you have any further queries.
Regards
I am using vb.net
example:
Dim dt as new datatable
If the string value is 2:32 then the datatable should have rows in time incrementing by around 15 minutes :
Having rows like :
2:45
3:00
3:15
example2:
String value is 4:34 then the datatable should have rows in time incrementing by around 15 minutes :
Having rows like :
4:45
5:00
5:15
Please let me know if you have any further queries.
Regards
ASKER
Is the "2:32" string part of some larger object? Or are you creating a new data table from one, standalone string value?
Yes,I am creating a new data table from one, standalone string value?
Also you said the "nearest 15 minutes" but for 2:32, the nearest 15 minute interval is 2:30. So are you wanting the next highest 15 minute interval?
So are you wanting the next highest 15 minute interval? Yes absolutely .
Thanks a lot for getting back
Yes,I am creating a new data table from one, standalone string value?
Also you said the "nearest 15 minutes" but for 2:32, the nearest 15 minute interval is 2:30. So are you wanting the next highest 15 minute interval?
So are you wanting the next highest 15 minute interval? Yes absolutely .
Thanks a lot for getting back
ASKER
Also gr8gonzo,
The incrementing should stop at 3:00 of next day.
example:
String value is 9:02 on Saturday then the rows in the datatable should be
09:15
09:30
09:45
'
'
03:00 Sunday
Thanks
The incrementing should stop at 3:00 of next day.
example:
String value is 9:02 on Saturday then the rows in the datatable should be
09:15
09:30
09:45
'
'
03:00 Sunday
Thanks
ASKER
Really appreciate your help .
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Yes definitely, will give a try and really appreciate your help!
Using Gonzo's proposal and adding in a DataTable extension method; this code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
namespace EE_Q29156752
{
class Program
{
static void Main(string[] args)
{
var start = "2:32";
var times = (from interval in Enumerable.Range(1, 48 * 60 / 15)
let origin = DateTime.Parse($"{DateTime.Parse(start).Hour}:{DateTime.Parse(start).Minute / 15 * 15}")
let endTime = new DateTime(origin.AddDays(1).Year, origin.AddDays(1).Month, origin.AddDays(1).Day, 15, 0, 0)
let startTime = DateTime.Parse($"{origin.Hour}:{origin.Minute / 15 * 15}").AddMinutes(interval * 15)
where startTime <= endTime
select new { StartTime = startTime }).ConvertToDataTable();
foreach (DataRow time in times.Rows)
{
Console.WriteLine(Convert.ToDateTime(time["StartTime"]).ToString("hh:mm:ss"));
}
Console.ReadLine();
}
}
static class Extensions
{
public static DataTable ConvertToDataTable<T>(this IEnumerable<T> source, string name = "")
{
DataTable table = new DataTable(name);
var properties = TypeDescriptor.GetProperties(typeof(T));
foreach (PropertyDescriptor property in properties)
{
if (property.PropertyType.IsGenericType && property.PropertyType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
{
table.Columns.Add(property.Name, property.PropertyType.GetGenericArguments()[0]);
}
else
{
table.Columns.Add(property.Name, property.PropertyType);
}
}
object[] values = new object[properties.Count];
foreach (var item in source)
{
for (int i = 0; i < properties.Count; i++)
{
values[i] = properties[i].GetValue(item);
}
table.Rows.Add(values);
}
return table;
}
}
}
Produces this output --saige-
ASKER
Sir,
Thanks a lot!!!
Will try and brb .
Thanks a lot!!!
Will try and brb .
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks a lot!
Also you said the "nearest 15 minutes" but for 2:32, the nearest 15 minute interval is 2:30. So are you wanting the next highest 15 minute interval?