How to delete data from selected Date to current Date using DateTimePicker - c#

My problem is to delete data from given date to current date,
My code works fine for deleting the data by the date given in the DateTimePicker.
i want to delete the data from given date in DateTimePicker to CurrentDate.
For example:
In subfolder123 the data is available from 20100131 to 20110531 (Date Format yyyyMMdd).
I want to delete the date from 20100215 to 20110531.
Hope you understood my Question and problem.
Is there any suggestions?
Here is my code:
private void button1_Click(object sender, EventArgs e)
{
string todaysDate = dateTimePicker1.Text;
int FinalDate4 = 0;
string Destinationnsefx = "C:\\folder\\subfolder\\subfolder123";
int xyz = 0;
string SecSym = (9722).ToString();
MWriterClass writerdelete1 = new MWriterClass();
try
{
writerdelete1.OpenDirectory(Destinationnsefx);
writerdelete1.OpenSecurityBySymbol(SecSym);
FinalDate4 = int.Parse(todaysDate);
if (writerdelete1.get_bDateExists(FinalDate4))
{
try
{
writerdelete1.DeleteIntradaySecRecordEx(FinalDate4, 080000, 240000);
}
catch
{
}
}
writerdelete1.CloseSecurity();
writerdelete1.CloseDirectory();
}
catch
{
}
}
Thanks in advance.

It sounds like you get a date from your DateTime picker and you need to work from that start date to your end date, correct?
In that case, look at the AddDays method for a DateTime object.
For example, this snippet of code will start at 4/1/2011 and print every date from then until today, in the format you specified.
var workingDate = new DateTime(2011, 4, 1);
while (workingDate < DateTime.Today)
{
workingDate = workingDate.AddDays(1);
Console.WriteLine(string.Format("{0:yyyyMMdd}", workingDate));
}

Related

Getting first and last date of selected month c# winforms

before getting started I want to tell that I already know how to select the 1st and last date of the current month. But, my question is different, I want to select the 1st and the last date of the selected month using date-time picker in c# winforms.
public Form1()
{
InitializeComponent();
MonthYearDT();
}
private string MonthYearDT()
{
dTMY.Format = DateTimePickerFormat.Custom;
dTMY.CustomFormat = "yyyy-MM-dd";
dTMY.ShowUpDown = true; // to prevent the calendar from being displayed
return dTMY.Value.ToString("yyyy-MM-dd");
}
private void BtnExecute_Click(object sender, EventArgs e)
{
.
.
.
.
monthYear = MonthYearDT();
.
.
.
}
In the above, the value of monthYear is 2019-12-19. It can be changed to any month. I want to get the 1st 2019-12-01 and last date 2019-12-31 of the selected month.
How can I do this?
Any help would be highly appreciated.
Before doing anything firstly change the function MonthYearDT() from string to DateTime
private DateTime MonthYearDT()
{
dTMY.Format = DateTimePickerFormat.Custom;
dTMY.CustomFormat = "yyyy-MM";
dTMY.ShowUpDown = true; // to prevent the calendar from being displayed
dTMY.Enabled = false;
return dTMY.Value;
}
Getting the first is easy. Just create a new DateTime like so
DateTime selectedDate = MonthYearDT();
var first = new DateTime(selectedDate.Year, selectedDate.Month, 1);
getting the second is just as easy. Use first, add one month and subtract one day like so
var second = first.AddMonths(1).AddDays(-1);
firstDate = first.ToString("yyyy-MM-dd");
lastDate = second.ToString("yyyy-MM-dd");
Universal truth is that the first day of any month would always be same.
And to get the last date below DateTime function can be used. You need year also along the month to get the last day
var lastDay = DateTime.DaysInMonth(year, month);
var lastDayDate = new DateTime(year, month, lastDay);

Get data day from date when selecting datagridview row

i have dgvData(datagridview), cmbPickRoom(combobox), numDay_In & numDay_Out(numericupdown) and code which like this
private void dgvData_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0)
{
DataGridViewRow row = this.dgvData.Rows[e.RowIndex];
cmbPickRoom.Text = row.Cells["Room"].Value.ToString();
numDay_In.Text = row.Cells["Day_In"].Value.ToString();
numDay_Out.Text = row.Cells["Day_Out"].Value.ToString();
}
}
while the data in mysql store date format(dd-mm-yyyy) for both "Day_In" and "Day_Out".
I want when i click dgvData, numDay_In and numDay_Out only take the day(dd)
If the database fields are of type DateTime, (Day_In and Day_Out) then you don't need to convert these values to string but to a DateTime variable, then getting the day is just a matter of reading a property
DateTime inDate = Convert.ToDateTime(row.Cells["Day_In"].Value);
numDay_In.Text = inDate.Day.ToString();
You can use DateTime.TryParse to convert the user entered string to date time format and then get the Day from it as I have done in the below code.
DateTime dt = new DateTime();
if(DateTime.TryParse(row.Cells["Day_In"].Value, out dt))
{
numDay_In.Text = dt.Day.ToString();
}
else
{
//Code to display error message
}
The added advantage of this code is that you can check if the user entered format is coorect, by checking the success of the TryParse in an if condition, If it fails, you can prompt the user to enter the Date in the correct format.

how to work with double in textbox?

I would like to write a code that reads a time the user wrote into textbox1, lets say 09:00 (hh:mm), save it into a variable, then takes this variable and adds 08:40 to it and Outputs then the outcome 17:40 into textbox2.
The Problem is, when working with double it is working well, but I need to use the hh:mm - Format, I know some languages have the Option to declare a variable as time but it seems like c# isnt doing this. so whats the solution?
thats my current code
void CmdWriteClick(object sender, EventArgs e)
{
string varstr = textbox_1.Text;
double muss = 8.40;
double vardb = Convert.ToDouble(varstr);
double end = vardb + muss;
textbox_2.Text = end.ToString();
}
this would of course Output me double-numbers. so if varstr is 08.30 it would Output 16,7 when I Need to make it Output 17:10. Any help is appreciated
thank you guys
You shouldn't be using a TextBox for this. You have a specific control to take care of dates and times: DateTimePicker.
Add one of these to your form. If you only want the user to modify the time, disallowing date changes, you can do the following:
public myForm()
{
InitializeComponent();
....
myDateTimePicker.Format = DateTimePickerFormat.Custom;
myDateTimePicker.CustomFormat = "HH:mm"; //Shows only hours and minutes in 24h format
myDateTimePicker.Value = DateTime.Now.Date; //sets the time to today at 0:00
myDateTimePicker.MinDate = DateTime.Now.Date;
myDateTimePicker.MaxDate = DateTime.Now.Date.Add(new TimeSpan(23, 59, 59)); //User can't change date.
}
private DateTime newTime;
private void myDateTimePicker_ValueChanged(object sender, EventArgs e)
{
newTime = myDateTimePicker.Value.AddHours(8.5);
}
Obviously if you want the user to set the date too, then simply change the MinValue and MaxValue restrictions to your requirements (if any) and choose one of the predefined format options instead of DateTimePickerFormat.Custom.
Try this :
dateValue.AddHours(8);
dateValue.AddMinutes(30);
You can use DateTime instead of double.
private void button1_Click(object sender, EventArgs e)
{
DateTime dateTime = DateTime.ParseExact(textBox1.Text, "HH:mm", System.Globalization.CultureInfo.CurrentCulture);
TimeSpan time = new TimeSpan(8,40,0);
DateTime outputTime = dateTime.Add(time);
textBox2.Text = outputTime.ToString("HH:mm");
}
You could do something like this:
string input = textbox_1.Text;
DateTime inputTime;
if (DateTime.TryParseExact(input, "HH:mm", CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out inputTime))
{
DateTime outputTime=inputTime.AddHours(8).AddMinutes(40);
textbox_2.Text = outputTime.ToString("HH:mm");
}
Basically, you parse the input string to a DateTime variable, add the time you need and then convert back to string to show it in your TextBox
If you really want to do it with double, you have to think about the conversion from minutes to whole numbers. 8.4 would be then 60 * .4 = 24 minutes.
For the rest, the text string you can parse using TimeSpan.Parse and you can write a custom function that would parse the double to a TimeSpan and then simply add the 2 together, like so:
using System;
namespace TimeParser {
class Program {
static TimeSpan GetTimeFromText( string text ) {
if ( string.IsNullOrWhiteSpace( text ) ) {
return TimeSpan.Zero;
}
return TimeSpan.Parse( text );
}
static TimeSpan GetTimeFromDouble( double value ) {
if ( value <= 0 ) {
return TimeSpan.Zero;
}
int hours = (int)Math.Floor( value );
int minutes = (int)(( value - hours )*60);
return new TimeSpan(0, hours, minutes, 0);
}
static TimeSpan GetAddedTime( string input, double time ) {
var textTime = GetTimeFromText( input );
return textTime.Add( GetTimeFromDouble( time ) );
}
static void Main( string[] args ) {
var totalTime = GetAddedTime( "8:30", 8.67 );
Console.WriteLine( totalTime ); // 17.10
}
}
}

I want to retrieve the current date and time nox textBox

protected void ButtonPesquisar_Click(object sender, EventArgs e)
{
var macroController = new MacroController();
var itens = macroController.ObterTodos(new Entities.FiltroMacro()
{
//NumeroLocomotiva = TextBoxNumeroLocomotiva.Text,
DataInicio = DateTime.Parse(TextBoxDataInicio.Text + " " + TextBoxHoraInicio.Text),
DataFim = DateTime.Parse(TextBoxDataFim.Text + " " + TextBoxHoraFim.Text)
my code now works as follows: inform the values ​​in textBox and returns me what is posted. But I'm wanting to return the current date and time of the system without I need to fill.
This is very simple, you could have just Googled this.
TextBoxHora.Text = DateTime.Today.ToString("g"); // Just the time
TextBoxData.Text = DateTime.Today.ToString("D"); // Just the Date
TextBoxDataHora.Text = DateTime.Today.ToString(); // Complete date and Time
TextBoxDataHoraCompleta.Text = DateTime.Now;
Go to these links for more information.
Link
Link
In the code I saw that you want the get the date from the text boxes, so: If you want to parse the date time and provide the format in one string you should use the DateTime.ParseExact method. For instance, see the usage in a sample console application:
static void Main(string[] args)
{
var parsedDate = DateTime.ParseExact("2014$05$01", "yyyy$MM$dd", DateTimeFormatInfo.CurrentInfo);
Console.WriteLine("Year: {0}",parsedDate.Year);
Console.WriteLine("Month: {0}",parsedDate.Month);
Console.WriteLine("Day: {0}",parsedDate.Day);
Console.ReadLine();
}
You can read Standard Date and Time Format Strings at msdn for searching more information about the date formats.

How to change "ddd" datetime format to a number view

I want to make that code to show me a current datetime on my pc clock when i click a button... i did it with that code listed down. But here is my question how can i make the "ddd" (day of a week) to be shown in number, not in words, I mean:
00-sunday
01-monday
and etc. ...
This is my code for the button:
private void SetClock_Click(object sender, EventArgs e)
{
SetClock.Click += new EventHandler(SetClock_Click);
{
DateTime time = DateTime.Now;
string format = "yy-MM-dd-ddd-hh-mm-ss";
txtSend.Text = time.ToString(format);
}
}
What i have to add to make it. Thanks
txtSend.Text = string.Format("{0:yy-MM-dd}-{1:00}-{0:hh-mm-ss}", time, (int)time.DayOfWeek);
A format isn't required, you can just cast DayOfWeek to an int:
var dayAsInt = (int)DateTime.Now.DayOfWeek;
Looking at the Custom Date and Time Format Strings page, there doesn't appear to be a format string for it :-(
The following should do it:
txtSend.Text =
time.ToString("yy-MM-dd-") +
((int)time.DayOfWeek).ToString("00") +
time.ToString("-hh-mm-ss");
You could use this to get the day of week numeric
int dayofweek = (int)DateTime.Now.DayOfWeek;

Categories