VB.net Addmonths - asp.net

I'm trying to move Dates, i.e. Go back a month or forward a month. If I move back a month as of this month (February) to January, it holds February's last day (28th) the same if I add a month. The start Date is fine as all month's start on the 1st.
Please could anyone advise.Thanks
Vb
hStartDate.Value = "2013/2/01 12:00:00 AM"
hEndDate.Value = "2013/2/28 11:59:59 PM"
Dim StartDate As New Date
StartDate = hStartDate.Value
hStartDate.Value = StartDate.AddMonths(-1)
Dim EndDate As New Date
EndDate = hEndDate.Value
hEndDate.Value = EndDate.AddMonths(-1)
Output
hStartDate.Value = "2013/01/01"
hEndDate.Value = "2013/01/28 11:59:59 PM"

Try this to set the end date (after hStartDate is modified):
hEndDate.Value = New Date(hStartDate.Year, hStartDate.Month, Date.DaysInMonth(hStartDate.Year, hStartDate.Month))
The function Date.DaysInMOnth() returns the last day of a month in a year given. You could set your date like this too if you prefer:
hEndDate.Value = EndDate.AddMonths(-1)
hEndDate.Value.Day = Date.DaysInMonth(hStartDate.Year, hStartDate.Month)

This should help: DevCurry it's basically taking the 1st day of the current month and removing one day thus resulting in the last day of the previous month.
hStartDate.Value = "2013/2/01 12:00:00 AM"
hEndDate.Value = "2013/2/28 11:59:59 PM"
Dim StartDate As New Date
StartDate = hStartDate.Value
hStartDate.Value = StartDate.AddMonths(-1)
Dim EndDate As New Date
EndDate = hEndDate.Value
hEndDate.Value = StartDate.AddDays(-1)

Related

Dart/Flutter get first DateTime of this week [duplicate]

This question already has answers here:
How to get start of or end of week in dart
(8 answers)
Closed 1 year ago.
Using flutter on android I am trying to get the first date of current week. For example today is 13/7/2020, assuming week starts from Saturday the first date of current week will be 11/7/2020.
This for example to get first date of month
DateTime firstDay = new DateTime(
DateTime.now().year,
DateTime.now().month,
1,
); //get first date this month
The end goal is to get timestamp of that date and fetch entries from sqflite database that is higher than that.
If Sunday is your first day of week.
var d = DateTime.now();
var weekDay = d.weekday;
var firstDayOfWeek = d.subtract(Duration(days: weekDay));
According to Flutter docs weekDay returns
The day of the week [monday]..[sunday].
In accordance with ISO 8601
a week starts with Monday, which has the value 1.
if Monday is first day of week then
var firstDayOfWeek = d.subtract(Duration(days: weekDay - 1));
For Sunday:
DateTime today = DateTime.now();
DateTime _firstDayOfTheweek =
today.subtract(new Duration(days: today.weekday));
print(_firstDayOfTheweek.day);
For Monday:
DateTime _firstDayOfTheweek =
today.subtract(new Duration(days: today.weekday - 1));
print(_firstDayOfTheweek.day);
For Saturday:
DateTime _firstDayOfTheweek =
today.subtract(new Duration(days: today.weekday + 1));
print(_firstDayOfTheweek.day);
The DateTime class stores int constants for the day of the week which you could use to calculate the start date you need. Something like this should work:
void main() {
var startOfWeek = DateTime.saturday;
var currentDate = DateTime.now();
var daysSince = currentDate.weekday + (DateTime.sunday - startOfWeek);
var result = currentDate.subtract(Duration(days: daysSince));
print('Date at start of week is $result');
}
To get different dates of different days in a week. We can use this isoweek package for this. We can use like this :-
Week currentWeek = Week.current();
Week weekFromIso = Week.fromISOString(currentWeek.toString());
print('Week from ISO string: $weekFromIso');
DateTime firstDay= weekFromIso.day(0);
String formattedDateFirst = DateFormat('EEE, d MMM').format(firstDay);
DateTime secondDay= weekFromIso.day(1);
String formattedDateSecond = DateFormat('EEE, d MMM').format(secondDay);

ASP.NET vb.net convert calendar date format in Month/Day/Year to Date object with Day/Month/Year

I have a calendar control:
<asp:Calendar ID="cldDepartDate" runat="server"></asp:Calendar>
And for example, if I chose 14th of March 2019, using the code below to display:
lblTest.Text = cldDepartDate.SelectedDate.ToString()
I got "3/14/2019 12:00:00 AM"
And now I want to convert it to "14/3/2019 12:00:00 AM" and store it into a Date object.
So far I have tried:
Dim oDate As Date = Date.ParseExact(cldDepartDate.SelectedDate.ToString("dd/MM/yyyy"), "MM/dd/yyyy", CultureInfo.InvariantCulture)
But it gives me this error:
Dim dateString = "3/14/2019 12:00:00 AM"
Dim oDate As Date = Date.ParseExact(dateString, "M/dd/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture)
Debug.Print(oDate.ToString("dd/M/yyyy hh:mm:ss tt"))
Using the standard DateTime formats.
https://learn.microsoft.com/en-us/dotnet/api/system.globalization.datetimeformatinfo?view=netframework-4.7.2#Formatting_dates_times

How do I convert a string date saved in seperate variables to a date using asp.net vb? The month variable is written out in text format

The data that I have to convert is written as the separate variables "Month", "Day", and "Year".
The data I need to convert for example is:
Month is "July"
Day is "21"
Year is "2013"
Combine it and use DateTime.ParseExact with CultureInfo.InvariantCulture:
Dim dtStr = String.Format("{0} {1} {2}", month, day, year)
Dim dt = Date.ParseExact(dtStr, "MMMM dd yyyy", CultureInfo.InvariantCulture)
Also have a look at: http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx
If you don't know if the data is valid you should use DateTime.TryParseExact:
Dim dt As DateTime
If Date.TryParseExact(dtStr, "MMMM dd yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, dt) Then
Console.Write("Date is: " & dt.ToShortDateString())
End If
I would first put the date into a string so
Dim dateString as string = string.format({0} {1} {2}, day, month, year)
Where day, month, year reference the variables holding the date values.
Then try
Dim result as DateTime = Convert.ToDateTime(dateString)

Checking the values of value of concated values of multiple dropdowns in VB.Net

I have 4 fields as:
• calStartDate = Control for the start Date Calendar (drop downs for Day, Month and Year)
• ddlTime = Start time drop down
• calEndDate = Control for the End Date Calendar (drop downs for Day, Month and Year)
• ddlTime2 = End time drop down
Now I have managed to save the following values in DB as;
• STARTDATETIME = “2/24/2012 6:30:00 AM”
• ENDDATETIME = “2/24/2013 6:30:00 AM”
As In the following code;
Dim EndDateTime As DateTime
Dim StartDateTime As DateTime
If chkbxAccAppBasedOnTimeInterval.Checked = True Then
Dim StartDate As System.DateTime? = calStartDate.CurrentDate
If StartDate.HasValue Then
StartDateTime = StartDate.Value.AddMinutes(CType(ddlTime.SelectedValue, Double))
EditRelTbl.STARTDATETIME = StartDateTime
End If
Dim EndDate As System.DateTime? = calEndDate.CurrentDate
If EndDate.HasValue Then
EndDateTime = EndDate.Value.AddMinutes(CType(ddlTime2.SelectedValue, Double))
EditRelTbl.ENDDATETIME = EndDateTime
End If
If EndDateTime > StartDateTime Then
DisplayAlert("End date time, must be less than Start Date and time", "Warning")
EndDateTime = StartDateTime
End If
Else
EditRelTbl.STARTDATETIME = Nothing
EditRelTbl.ENDDATETIME = Nothing
End If
Issue is that I have to check that STARTDATETIME must be greater than ENDDATETIME and if the End dateTime is greater than Start DateTime then the fill the End datetime as Start DateTime as I tried to done in the code
If EndDateTime > StartDateTime Then
DisplayAlert("End time must be greater than Start time", "Warning")
EndDateTime = StartDateTime
End If
But this not checking the check I want. Can anyone guide me how to perform the check, or where I am mistaken.
Note: ‘EditRelTbl’ is the table, where I am saving the values of End/Start date time.

Date format in vb.net

I am using the following function to Convert a String variable to a Date. Subtract a day from it and convert the date back to a String. The code goes as follows
Dim edate As String
Dim expenddt As Date
edate = txtenddt.Text
expenddt = Date.ParseExact(edate, "dd/MM/yyyy", System.Globalization.DateTimeFormatInfo.InvariantInfo)
expenddt = expenddt.AddDays(-1)
Dim asd As String = expenddt.ToString
If edate has a value 29/12/2011 than the value in expenddt gets changed to a different format and the value in expenddt comes to 12/29/2011 and later after subtracting a day expenddt is 12/28/2011 and than when i convert it back to a String i get the value in asd as "12/28/2012 12:00:00 AM"
I have changed the date format on my system to d/M/yyyy in Regional And Language Option in Control Panel but i still get a different format in expenddt
Can anyone explain me why this is happening? How can i keep the format of the date in dd/mm/yyyy e.g 29/12/2011 and after
Subtracting a day it should remain 28/12/2011 and not 12/29/2011
Use Date.ToString() to convert date to string with dd/MM/yyyy format.
Dim asd As String = expenddt.ToString("dd/MM/yyyy")
Just try this:
Dim dateString As String = "Mon 16 Jun 8:30 AM 2008"
Dim format As String = "ddd dd MMM h:mm tt yyyy"
Dim dateTime__1 As DateTime = DateTime.ParseExact(dateString, format, CultureInfo.InvariantCulture)
hope this may helpful....

Resources