Is it possible to convert date 4 digit year 2 digit month and 2 digit day to dd (3 digit month) 4 digit year?
Right now I have the input of date "use date" as user entered YYYYMMDD. I prefer to use the calendar input as it keeps the date consistent
A Date/Time value is actually a double precision float number.
So you can take a number, and use CDate to represent it as a date.
? CDate(41668.0)
1/29/2014
The display format of the date value is a separate issue. The same numeric date value can be displayed in whatever format you prefer.
? Format(CDate(41668.0), "yyyymmdd")
20140129
? Format(CDate(41668.0), "dd mmm yyyy")
29 Jan 2014
But the actual date value (the number) is unchanged --- that number doesn't get "converted" regardless of how it's displayed.
If your issue is that the users are working with a text value instead of a Date/Time value, you either have to convert that text to a valid Date/Time value or modify your application so they enter Date/Time values instead of text.
The second alternative is less fuss. But if you're stuck with dates as text, you can do something like this ...
use_date = "20140129"
' transform it to a string CDate can accept ...
? Left(use_date, 4) & "-" & Mid(use_date, 5, 2) & "-" & Right(use_date, 2)
2014-01-29
' get the date from that string ...
? CDate(Left(use_date, 4) & "-" & Mid(use_date, 5, 2) & "-" & Right(use_date, 2))
1/29/2014
' finally make it a string in your desired format ...
? Format(CDate(Left(use_date, 4) & "-" & Mid(use_date, 5, 2) & "-" & Right(use_date, 2)), "dd mmm yyyy")
29 Jan 2014
try
DIM DateStr : DateStr = "20140119" 'Your date
Response.Write "DEBUG: DateStr = " & DateStr & "<br>"
'Split number so can use Date functions
DIM NewDate : NewDate = DateSerial(CInt(Mid(DateStr, 1, 4)), CInt(Mid(DateStr, 5, 2)), Mid(DateStr, 7, 2))
Response.Write "DEBUG: NewDate = " & NewDate & "<br>"
TheDate=CDate(NewDate)
Response.Write "DEBUG: CDate(NewDate) = " & TheDate & "<br>"
DIM FinalDate: FinalDate = DatePart("d", TheDate) & " "
FinalDate = FinalDate & MonthName(Month(TheDate),1) & " "
FinalDate = FinalDate & DatePart("yyyy", TheDate)
Response.write "DEBUG: Required Date = " & FinalDate
Related
converting string variable which contains date '10/13/2019 12:16:58 PM' to MySQL date formate ('yyyyy-mm-dd HH:mm:ss tt") vb.net to MySQL format
I have variable which contain date value as 10/13/2019 12:16:58 PM
I want to convert it into MySQL format as ('yyyyy-mm-dd HH:mm:ss tt')
how to do it in vb.net
any help
I have searched net but not worked,
I want to convert just variable value
sqlquerym = "Select *
FROM registration
where pcname='" & strHostName & "' and ip='" & strIPAddress & "' and timestap='" & lbltimestap.Text & "' and serialnumber='" & sysserial & "'
ORDER BY id limit 1"
where lbltimestap.Text contains string value of date as '10/13/2019 12:16:58 PM' but in mysql table it is stored as 2019-10-13 12:16:58 so no results shown
As commented by Tim Biegelsen, instead of doing the conversion in VB, you can use STR_TO_DATE() to convert the date string to a MySQL date, as follows:
sqlquerym =
"Select * FROM registration where "
& "pcname='" & strHostName & "' "
& "and ip='" & strIPAddress & "' "
& "and timestap=STR_TO_DATE('%d/%m/%Y %r', '" & lbltimestap.Text & "') "
& "and serialnumber='" & sysserial & "' "
& "ORDER BY id limit 1"
Details:
%d: Day of the month, numeric (01..31)
%m: Month, numeric (01..12)
%Y: Year, numeric, four digits
%r: Time, 12-hour (hh:mm:ss followed by AM or PM)
The simpest way is to convert your variable to a date
and then use
dataRow("timecolumn") = datevariable.ToString("yyyy-MM-dd HH:mm:ss.fff")
Note: SMonth is date to and SYear is date from. I just haven't changed the names but the format has already been changed to short date.
I've created a code in Microsoft Access Visual Basic to create a date range but when I type the date range in both text boxes SMonth and SYear, it only shows the records with dates from txtYear and not the range date itself. This is the code.
'Date Range
ElseIf Me.SEmployeeName = "" And Me.Soo = False And Me.Scc = False And
Me.SMonth <> "" And Me.SYear <> "" Then
Me.tbl_ALL_Query_subform.Form.RecordSource = "Select * from tbl_ALL where
((((tbl_ALL.NTEDate ) <= '" & Me.SMonth.Value & "') AND (tbl_ALL.NTEDate) >=
'" & Me.SYear.Value & "')) "
Your txtTo and txtFrom don't seem to come into play, so hard to tell.
However, you compare date with text, so adjust your query:
ElseIf Me.SEmployeeName = "" And _
Me.Soo = False And Me.Scc = False And _
Me.SMonth <> "" And Me.SYear <> "" Then
Me!tbl_ALL_Query_subform.Form.RecordSource = _
"Select * from tbl_ALL where Month(tbl_ALL.NTEDate) <= " & Me!SMonth.Value & " AND Year(tbl_ALL.NTEDate) >= " & Me!SYear.Value & ""
If start date and end date:
"Select * From tbl_ALL Where tbl_ALL.NTEDate Between #" & Format(Me!StartDate.Value, "yyyy\/mm\/dd") & "# And #" & Format(Me!EndDate.Value, "yyyy\/mm\/dd") & "#"
I have a program I am working on where all the events on given dates in a SQL DB populate a calendar. I recently switched from MYSQL to MSSQL 2012 and I am now getting the error "Conversion failed when converting date and/ or time from character string."
here is the code that forms the date
'// Set Start and End Date
If numericMonth < 10 Then
doubleMonth = "0" & numericMonth 'If Month was September doubleMonth = 09
startDate = numericYear & "-" & doubleMonth & "-01" ' If date was June 1 2015 startdate would = 2015-06-01
Calendar1.Refresh()
Else
startDate = numericYear & "-" & numericMonth & "-01"
doubleMonth = numericMonth
End If
Dim endDate As String
If numericMonth < 10 Then
endDate = numericYear & "-0" & numericMonth & "-31"
Else
endDate = numericYear & "-" & numericMonth & "-31" ' If date was June 30 2015 enddate would = 2015-06-31
End If
If bypassMode = "0" Then
count = 0
dbQuery = "SELECT * FROM SOEVENTS WHERE DATE BETWEEN '" & startDate & "' AND '" & endDate & "' ORDER BY date ASC"
If SQL.HasConnection = True Then
SQL.RunQuery(dbQuery)
End If
The final Outcome is formatted yyyy-mm-dd. All future months work but it seems every 3rd month in the past does not.
The efficiency and security of your code notwithstanding, the obvious problem is that 5 of the 12 months do not have a 31st day and in these cases you are trying to assign dates that are invalid. VB knows how to count calendar units (e.g. months) properly when you use its methods, but assigning an invalid date from a string produces an error. Try this instead:
Dim endDate As DateTime = startDate.AddMonths(1).AddDays(-1)
I'm struggling with a problem about Dates manipulation.
Background :
Access 2013 - Classic Stock Table (id, name_item, record_date)
Currently working on SearchForm.
Have to finish the last filter (dates) - Others filters work.
I designed a ComboBox to filter "< 7 days", "< 30 days",...
PBM :
Date format is d/m/y (french format)
Operator < seems to compare with m/d/y even my date are defined in d/m/y.
I verified both Type in Table and Date(), they're set in d/m/y
CODE :
If InputDate.Value = "La semaine derniere" Then
strfilter = strfilter & "([Record_date] > #" & DateAdd("d", -7, Date) & "#)"
Debug.Print Month(DateAdd("d", -7, Date)) & " " & strfilter
Output :
9 ([Record_date] > #03/09/2015#)
I tried also strfilter = strfilter & "(Datediff('d', date(), [Record_date]) < 7)"
No difference :'(.
Any idea how I can go through this problem please.
Falt
You need the date value as a string expression formatted to yyyy/mm/dd:
strfilter = strfilter & "([Record_date] > #" & Format(DateAdd("d", -7, Date), "yyyy\/mm\/dd" & "#)"
I'm trying to filter a form between dates - first and last of the next month. My coding for the last seems to work.I have also made a text box which does return the date I want but the same code in the filter is returning dates from 01 Jan 2013.
Public Function DtFrom() As Date
DtFrom = DateSerial(Year(Date), Month(Date) + 1, 1)
End Function
Me.Filter = "[DateDue] Between #" & DtFrom & "# And #" & (DateSerial(Year(Date), Month(Date) + 2, 1) - 1) & "#"
"[DateDue] Between #" & DtFrom() & "# And #" &
make sure you are calling the function DtFrom() 'yes you need the parenthesis