I need to compare dates in winforms, using vb.net
I am trying using
If Now() = DateTime.Parse("17.01.2013 08:47:10 PM")
The problem is my date is in dd/mm/yyyy format but when this application runs on a system where the format is mm/dd/yy, it gives problem.
How can I bring the system date (obtained using NOW()) in dd/mm/yyyy format so it could be compared with my given date?
Thanks
Try using DateTime.ParseExact or TryParseExact:
Dim date As Datetime = DateTime.ParseExact(_
"17.01.2013 08:47:10 PM", "dd.MM.yyyy hh:mm:ss tt", CultureInfo.InvariantCulture)
Good luck.
I try to use the format "yyyy-MM-dd HH:mm:ss" as this works with any dateformat and also with SQL. See below:
Private Shared Sub DateCompare()
Dim myDate As Date = "12/07/2013 21:31:34"
Dim nowDateString As String = Format(Now, "yyyy-MM-dd HH:mm:ss")
Dim myDateString As String = Format(myDate, "yyyy-MM-dd HH:mm:ss")
If nowDateString = myDateString Then
MessageBox.Show("Matches")
End If
End Sub
Try something like that:
if (Now().ToString("MM/dd/yy") = myDate)
Careful of "MM" and "mm" in vb formatting.
Related
I'd like to achieve the result seen in A1. How come line 3 doesn't output the same result as line 1 & 2 even though the format is the same - yyyy-mm-dd hh:mm:ss?
Does this have anything to do with regional settings?
1 Range("A1").Value2 = Now
2 Range("A1").NumberFormat = "yyyy-mm-dd hh:mm:ss"
3 Range("B1").Value2 = Format(Now, "yyyy-mm-dd hh:mm:ss")
That is because:
A1: You set a Date value and apply a specific format for display.
B1: You format a Date value to Text, set that as the value, but the cell has been applied no specific format, thus Excel sees the text date as a date value and casts it to a true Date value, which is displayed with your default (German?) date format.
To force the text date to be read as text, you could prefix it with a quote:
Range("A1").Value2 = Now
Range("A1").NumberFormat = "yyyy-mm-dd hh:mm:ss"
Range("B1").Value2 = Format(Now, "yyyy-mm-dd hh:mm:ss")
Range("C1").Value2 = Format(Now, "\'yyyy-mm-dd hh:mm:ss")
Output (Danish localisation):
Note, that C1 is text (left-aligned).
Why you not set, before or after, your cell formatting with a Range?
Example:
Range("A1:Z1").NumberFormat = "yyyy-mm-dd hh:mm:ss"
Range("A1").Value2 = Now
Range("B1").Value2 = Now
I'm struggeling to format my date strings correctly as it appears to be minusing the wrong parts of my date using DateAdd and I'm not sure how to resolve.
Eg:
Sub DateTest()
DateStr = Format(Date, "DD-MM-YY")
Yesterday = Format(DateAdd("d", -1, CDate(DateStr)), "DD-MM-YY")
YtdStr = Format(Yesterday, "DD-MM-YY")
Debug.Print DateStr
Debug.Print Yesterday
Debug.Print YtdStr
End Sub
Result:
13-09-20
19-09-13
13-09-19
Expected Result:
13-09-20
12-09-20
12-09-20
I even tried using just "day of year" as this is for an hidden report, but I like using regular date strings I think or at least I'd like to get both figured out, but using day of year showed me some interesting results. Yesterday had to be d 0 which made no sense, but if I used -1 it removed two dates. As well, turning that into a string seems to remove another day?
Eg:
Sub DateTest()
DateStr = Format(Date, "Y")
YtdDate = Format(DateAdd("d", 0, CDate(DateStr)), "Y")
YtdStr = Format(YtdDate, "Y")
Debug.Print DateStr
Debug.Print YtdDate
Debug.Print YtdStr
End Sub
Resut:
257
256
255 'This was expected to be a string 256?
Can anybody point out how to format this correctly?
You are formatting the source date as string using DD-MM-YY, parsing it back to date using your current system default date format (apparently MM-DD-YY), adding days to the result of that, and formatting it back to string. You don't want these intermediate formatting to strings.
Sub DateTest()
Dim DateNotStr As Date
Dim YesterdayNotStrEither As Date
DateNotStr = Date
YesterdayNotStrEither = DateAdd("d", -1, DateNotStr)
Debug.Print Format$(DateNotStr, "dd-mm-yy")
Debug.Print Format$(YesterdayNotStrEither , "dd-mm-yy")
End Sub
I think the issue was CDate wasn't transforming my date correctly based on my systems default date format. I created a variable for today in date format and used that to generate yesterdays variable.
Eg:
Public Sub SetDateVars()
TdDate = Date
TdStr = Format(Date, "DD-MM-YY")
YtdDate = Format(DateAdd("d", -1, TdDate))
YtdStr = Format(YtdDate, "DD-MM-YY")
Debug.Print TdDate
Debug.Print TdStr
Debug.Print YtdDate
Debug.Print YtdStr
End Sub
Result:
9/13/2020
13-09-20
9/12/2020
12-09-20
I am currently running this code, and it displays the date as Output shown below.
Dim started As DateTime = DateTime.Now
Dim date As DateTime = started.Add(diaryItem.Duration)
Output : 23/03/2016 17:00:00
Is it possible to remove the date and only show the time ?
If you only want to see time from datetime then try this:
'show time
Dim OnlyTime As String = DateTime.Now.ToString("hh:mm:ss")
'or
Dim OnlyTime = DateTime.Now.ToShortTimeString
If you only want to see date from datetime then try this:
'show date
dateOnly = myDateTime.Date
'or
dateString = dateOnly.ToShortDateString()
If this is not what you are looking for then put more informations..
DateTime Structure
I want to display date in 09/07/2013 format instead of 09-jul-13.
Dim dt As Date = Date.Today
MsgBox(dt)
First, uppercase MM are months and lowercase mm are minutes.
You have to pass CultureInfo.InvariantCulture to ToString to ensure that / as date separator is used since it would normally be replaced with the current culture's date separator:
MsgBox(dt.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture))
Another option is to escape that custom format specifier by embedding the / within ':
dt.ToString("dd'/'MM'/'yyyy")
MSDN: The "/" Custom Format Specifier:
The "/" custom format specifier represents the date separator, which
is used to differentiate years, months, and days. The appropriate
localized date separator is retrieved from the
DateTimeFormatInfo.DateSeparator property of the current or specified
culture.
Try this.
var dateAsString = DateTime.Now.ToString("dd/MM/yyyy");
// dateAsString = "09/07/2013"
and also check this link for more formatting data and time
Like this ..
MsgBox(format(dt,"dd/MM/yyyy"))
You could decompose the date into it's constituent parts and then concatenate them together like this:
MsgBox(Now.Day & "/" & Now.Month & "/" & Now.Year)
Dim formattedDate As String = Date.Today.ToString("dd/MM/yyyy")
Check link below
I found this catered for dates in 21st Century that could be entered as dd/mm or dd/mm/yy. It is intended to print an attendance register and asks for the meeting date to start with.
Sub Print_Register()
Dim MeetingDate, Answer
Sheets("Register").Select
Range("A1").Select
GetDate:
MeetingDate = DateValue(InputBox("Enter the date of the meeting." & Chr(13) & _
"Note Format" & Chr(13) & "Format DD/MM/YY or DD/MM", "Meeting Date", , 10000, 10000))
If MeetingDate = "" Then GoTo TheEnd
If MeetingDate < 36526 Then MeetingDate = MeetingDate + 36526
Range("Current_Meeting_Date") = MeetingDate
Answer = MsgBox("Date OK?", 3)
If Answer = 2 Then GoTo TheEnd
If Answer = 7 Then GoTo GetDate
ExecuteExcel4Macro "PRINT(1,,,1,,,,,,,,2,,,TRUE,,FALSE)"
TheEnd:
End Sub
if you want to display date along with time when you export to Excel then you can use this
xlWorkSheet.Cells(nRow, 3).NumberFormat = "dd/mm/yy h:mm AM/PM"
I'm getting date-time from a source as 20110731183330. I want to convert it to '7/31/2011 06:33:30 PM'. Is there a one line code for the same in vb.net?
That would be:
Dim result As Date = Date.ParseExact(datestring, "yyyyMMddHHmmss")
It can be...
Dim CustomDate As Date = Date.ParseExact(yourdate, "MM/dd/yyyy hh:mm:ss tt", Globalization.DateTimeStyles.None)
Dim result As Date = Date.ParseExact(dt, "yyyyMMddHHmmss", System.Globalization.CultureInfo.InvariantCulture)
then we can format it as required.