Example Data Link - Sorry, wouldn't format correctly when pasted.
1.
Dec 01, 2011
06:00:00 AM
Dec 01, 2011
07:05:00 AM
65
2.11
2.
Dec 01, 2011
06:00:00 PM
Dec 01, 2011
07:05:00 PM
65
2.11
3.
Dec 02, 2011
06:05:00 AM
Dec 02, 2011
07:05:00 AM
60
1.95
I'd like each separate line to have its own place in an array or datatable, but I can't seem to get it to work correctly. There must be something different with the ending characters?
Code is below:
Dim strOutput As String = ""
'' Demo Data
'Dim strData As String = "59. Dec 01, 2011 06:05:00 PM Dec 01, 2011 10:05:00 PM 240 80.00"
'strOutput = +FormatRow(strData)
'' Demo Data
Dim sFileName As String = OpenFileDialog.FileName
If My.Computer.FileSystem.FileExists(sFileName) Then
Dim srFileReader As System.IO.StreamReader
Dim sInputLine As String
srFileReader = System.IO.File.OpenText(sFileName)
sInputLine = srFileReader.ReadLine()
Do Until sInputLine Is Nothing
'strOutput = +FormatRow(sInputLine)
Dim title As String = srFileReader.ReadLine()
Dim startTime As DateTime = srFileReader.ReadLine() & " " & srFileReader.ReadLine()
Dim endTime As DateTime = srFileReader.ReadLine() & " " & srFileReader.ReadLine()
Dim timeSpan As TimeSpan = endTime.Subtract(startTime)
Dim minutesTotal As Integer = timeSpan.TotalMinutes
' Burn Minutes
srFileReader.ReadLine()
Dim billMinutes As Integer = minutesTotal
Dim billTotal As Double = srFileReader.ReadLine()
strOutput += ""
Loop
Output something like:
12/1/2011 6:00:00 AM 12/1/2011 7:05:00 AM 65 65 2.11
You have read while text file at once and I think File.ReadAllLines will be good choice.
Dim str
Dim fileName = "C:\SampleData.txt"
Dim lines() = File.ReadAllLines(fileName)
For i = 0 To lines.GetUpperBound(0) Step 7
str = String.Format("{0}{1}{2}{3}{4}{5}",
lines(i), lines(i + 1), lines(i + 2), lines(i + 3),
lines(i + 4), lines(i + 5))
Console.WriteLine(str)
Next
Related
I have kind of difficult question.
I have a a string like
2021-09-24 00:52:27
2021-09-23 03:46:08
2021-09-22 04:13:03
2021-09-21 04:08:02
2021-09-20 02:13:27
2021-09-19 05:27:38
2021-09-18 03:43:32
2021-09-17 03:23:44
I need to count the timespan of a last week.
The thing the string not always will be the last 7 days
so i made a string to get the last 7 days and compare them and if a line isnat matching the date it will be 00:00:00,
before that i splited the string.
before that i declared X as a number of lines in the string
2021-09-24
2021-09-23
2021-09-22
2021-09-21
2021-09-20
2021-09-19
2021-09-18
2021-09-17
00:52:27
03:46:08
04:13:03
04:08:02
02:13:27
05:27:38
03:43:32
03:23:44
Dim x As String = RichTextBox4.Lines.Count ' The number of lines with date and hours together
Dim a1 As String = Date.Now.ToString("yyyy-MM") ' Creating dates for the last 7 days
Dim a2 As String = Date.Now.ToString("dd")
Dim b1 As String = Date.Now.ToString("yyyy-MM")
Dim b2 As String = Date.Now.ToString("dd") - 1
Dim c1 As String = Date.Now.ToString("yyyy-MM")
Dim c2 As String = Date.Now.ToString("dd") - 2
Dim dd1 As String = Date.Now.ToString("yyyy-MM")
Dim dd2 As String = Date.Now.ToString("dd") - 3
Dim e1 As String = Date.Now.ToString("yyyy-MM")
Dim e2 As String = Date.Now.ToString("dd") - 4
Dim f1 As String = Date.Now.ToString("yyyy-MM")
Dim f2 As String = Date.Now.ToString("dd") - 5
Dim g1 As String = Date.Now.ToString("yyyy-MM")
Dim g2 As String = Date.Now.ToString("dd") - 6
Dim d1 As String = a1 + "-" + a2
Dim d2 As String = b1 + "-" + b2
Dim d3 As String = c1 + "-" + c2
Dim d4 As String = dd1 + "-" + dd2
Dim d5 As String = e1 + "-" + e2
Dim d6 As String = f1 + "-" + f2
Dim d7 As String = g1 + "-" + g2
Dim lines11 = RichTextBox4.Lines
Dim modifiedLines11 = New List(Of String)()
For index11 As Integer = 0 To lines11.Length - 1
Dim url11 = lines11(index11).Substring(lines11(index11).LastIndexOf(" "c))
modifiedLines11.Insert(index11, lines11(index11).Replace(url11, String.Empty))
modifiedLines11.Add(url11)
RichTextBox4.Text = String.Join(Environment.NewLine, modifiedLines11)
Next
' after split to 2 lists in string
Dim o1 As String = RichTextBox4.Lines(0) ' The first lines of the string is the dates
Dim o2 As String = RichTextBox4.Lines(1)
Dim o3 As String = RichTextBox4.Lines(2)
Dim o4 As String = RichTextBox4.Lines(3)
Dim o5 As String = RichTextBox4.Lines(4)
Dim o6 As String = RichTextBox4.Lines(5)
Dim o7 As String = RichTextBox4.Lines(6)
Dim l1 As String ' This is the second list in the string those are the times
Dim l2 As String
Dim l3 As String
Dim l4 As String
Dim l5 As String
Dim l6 As String
Dim l7 As String
here im trying to compare it to the actual date and if isnt it will give me 00:00:00.
If o1 = d1 Then
l1 = RichTextBox4.Lines(x)
Else
l1 = "00:00:00"
If o2 = d2 Then
l2 = RichTextBox4.Lines(x + 1)
Else
l2 = "00:00:00"
If o3 = d3 Then
l3 = RichTextBox4.Lines(x + 2)
Else
l3 = "00:00:00"
If o4 = d4 Then
l4 = RichTextBox4.Lines(x + 3)
Else
l4 = "00:00:00"
If o5 = o5 Then
l5 = RichTextBox4.Lines(x + 4)
Else
l5 = "00:00:00"
If o6 = d6 Then
l6 = RichTextBox4.Lines(x + 5)
Else
l6 = "00:00:00"
If o7 = d7 Then
l7 = RichTextBox4.Lines(x + 6)
Else
l7 = "00:00:00"
End If
End If
End If
End If
End If
End If
End If
' here im trying to calculate the time.
Dim ts1 As TimeSpan = TimeSpan.Parse(l1)
Dim ts2 As TimeSpan = TimeSpan.Parse(l2)
Dim ts3 As TimeSpan = TimeSpan.Parse(l3)
Dim ts4 As TimeSpan = TimeSpan.Parse(l4)
Dim ts5 As TimeSpan = TimeSpan.Parse(l5)
Dim ts6 As TimeSpan = TimeSpan.Parse(l6)
Dim ts7 As TimeSpan = TimeSpan.Parse(l7)
Dim ttsum As TimeSpan = ts1 + ts2 + ts3 + ts4 + ts5 + ts6 + ts7
MsgBox(ttsum.ToString)
In my opinion the porblem with the code is or the method is bad or the if statements arent good with this method.
Since I don't see any AM/PM data, I am assuming the data in the rich text box is in 24 hour format.
Your code is so complicated because you are manipulate strings instead of dealing with dates. If you want to compare dates and manipulate dates, the first thing you need to do is turn your strings into dates. Since we do not know how many items we are dealing with we will use List(Of T).
Private Function GetListOfDates() As List(Of Date)
Dim lst As New List(Of Date)
Dim provider As CultureInfo = CultureInfo.InvariantCulture
For Each line In RichTextBox1.Lines
' 2021-09-17 03:23:44
lst.Add(Date.ParseExact(line, "yyyy-MM-dd hh:mm:ss", provider))
Next
Return lst
End Function
Next you need to filter the list for last week. For your purposes, a day starts as 00:00:00 and ends at 23:59:59
Private Function FilterListForLastWeek(lst As List(Of Date)) As List(Of Date)
Dim DatesInWeek As New List(Of Date)
'I hardcoded the following date to work with your sample data
Dim LastDayOfWeek = New Date(2021, 9, 24, 23, 59, 59)
'You would use the following code in the real version
'Dim LastDayOfWeek = New Date(Date.Now.Year, Date.Now.Month, Date.Now.Day, 23, 59, 59)
Debug.Print(LastDayOfWeek.ToString)
Dim FirstDayOfWeek = LastDayOfWeek.AddDays(-6)
FirstDayOfWeek = New Date(FirstDayOfWeek.Year, FirstDayOfWeek.Month, FirstDayOfWeek.Day, 0, 0, 0)
Debug.Print(FirstDayOfWeek.ToString)
For Each d In lst
If d >= FirstDayOfWeek AndAlso d <= LastDayOfWeek Then
DatesInWeek.Add(d)
End If
Next
Return DatesInWeek
End Function
It looks like you are considering the hour, minute, second portion of the date as a time span. To add TimeSpan you use the Add method. The last date in the data from the rich text box (the 17th) is excluded since it is not in the week (days 18-24 inclusive is 7 days). The message box displays 1:00:24:17 which is 1 day, zero hours, 24 minutes, and 17 seconds. You can also display as TotalHours, TotalMinutes, etc..
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim AllDates = GetListOfDates()
Dim DatesInWeek = FilterListForLastWeek(AllDates)
Dim TimeSpanSum As TimeSpan
For Each d In DatesInWeek
TimeSpanSum = TimeSpanSum.Add(New TimeSpan(d.Hour, d.Minute, d.Second))
Next
MessageBox.Show(TimeSpanSum.ToString)
End Sub
How do i convert day/Month to Julian Day in visual basic?
Formula for converting in other languages are also appreciated.
For e.g
Julian Day for 1 Feb=032
https://landweb.modaps.eosdis.nasa.gov/browse/calendar.html
From research in the web ,most resolutions are converting the date with yyyy-mm-dd to julian date.
As the example above, i need to get value 032
The .Net JulianCalendar Class exposes all methods needed to achieve your goal. It is also COM visible, so you can reference it in a VBA project after adding a project reference to "mscorlib.dll".
In VB.Net the code would be:
Dim jc As New System.Globalization.JulianCalendar
' create a gregorian equivalent of the julian date: 1 Feb 2018
Dim gregoriaDateEquivalent As DateTime = jc.ToDateTime(2018, 2, 1, 0, 0, 0, 0)
' = #2/14/2018 12:00:00 AM#
Dim dayOfYear As Int32 = jc.GetDayOfYear(gregoriaDateEquivalent)
' = 32
In VBA the code would be:
Dim jc As mscorlib.JulianCalendar
Set jc = New mscorlib.JulianCalendar
' create a gregorian equivalent of the julian date: 1 Feb 2018
Dim gregoriaDateEquivalent As Date
gregoriaDateEquivalent = jc.ToDateTime(2018, 2, 1, 0, 0, 0, 0)
' = #2/14/2018
Dim dayOfYear As Long
dayOfYear = jc.GetDayOfYear(gregoriaDateEquivalent)
' = 32
First you have to add reference to mscorlib library
Under VB6,Project-References->Tick the mscorlib checkbox
Create this function method in the object that you ahve or otherwise create a sub procedure
Public Function JulianConverter() As String
'MsgBox Fix(5.2), vbExclamation, App.Title
'MsgBox Fix(5.6)
Dim y As String
Dim m As String
Dim d As String
Dim strDate As String
Dim dateArray() As String
strDate = Format(Now, "dd/MMM/yyyy")
dateArray = Split(strDate, "/")
d = dateArray(0)
m = dateArray(1)
y = dateArray(2)
'Debug
'MsgBox strDate
'Convert to Integer
y = Val(y)
m = Val(m)
d = Val(d)
If m <= 2 Then
y = y - 1
m = m + 12
End If
'Dim A As Double
' Dim B As Double
'Dim JD As Double
' A = CDbl(Fix(y / 100#))
'B = 2 - A + Fix(A / 4)
'JD = Fix(365.25 * (y + 4716)) + Fix(30.6001 * (m + 1)) + d + B - 1524.5
'JulianConverter = CStr(JD) 'Convert to string
Dim jc As mscorlib.JulianCalendar
Set jc = New mscorlib.JulianCalendar
' create a gregorian equivalent of the julian date: 1 Feb 2018
Dim gregoriaDateEquivalent As Date
gregoriaDateEquivalent = jc.ToDateTime(2018, m, d, 0, 0, 0, 0)
' = #2/14/2018
Dim dayOfYear As Long
Dim dayOfYearS As String
Dim digitLength As Integer
Dim Counter As Integer
dayOfYear = jc.GetDayOfYear(gregoriaDateEquivalent)
'Have to ensure 3 digits values
dayOfYearS = CStr(dayOfYear)
'Count number of Digits of string
digitLength = Len(dayOfYearS)
MsgBox "DigitLength" & digitLength, vbExclamation, App.Title
'If Digits length smaller than 3,add one 0 in front
If (digitLength < 3) Then
dayOfYearS = "0" & dayOfYearS
End If
JulianConverter = dayOfYearS
End Function
This will ensure the julian day will be 3 digits in value based on the current system date
I have written a program which calculates the time difference between two times.
It calculates the time difference between upto some extent (or few cells appropriately). After few cells it writes garbage values to the rest of the cells.
Please help me.
See the code below.
Sub Average()
Dim LogIn As String
Dim LogOff As String
Dim Row As Integer
Dim Col As Integer
Dim InTime As Date
Dim OffTime As Date
Row = 1
Col = 2
While (Cells(Row, Col) <> "")
Workbooks("Log-In-Time.xlsm").Activate
InTime = Cells(Row, Col)
Workbooks("Log-Off-Time.xlsm").Activate
OffTime = Cells(Row, Col)
Workbooks("Log-In-Time.xlsm").Activate
Cells(Row, Col + 1) = CDate(OffTime) - CDate(InTime)'<- Without CDate also I have tried but output was same.
Row = Row + 1
Wend
End Sub
My Log-In-Time.xls content is,
OUTPUT
7/11/2013 11:35:41 AM 7:14:15 AM
7/15/2013 11:05:22 AM 10:03:00 AM
7/16/2013 9:58:25 AM 11:11:31 AM
7/17/2013 10:33:20 AM 10:39:25 AM
7/18/2013 11:10:33 AM 6:58:35 AM
7/19/2013 12:18:59 AM 7:18:09 PM <-----Here onwadrs
7/22/2013 11:58:26 AM 0.370185185
7/23/2013 11:27:14 AM 0.418645833
7/24/2013 10:59:36 AM 0.439953704
7/25/2013 11:20:16 AM 0.382650463
7/26/2013 11:09:14 AM 0.373171296
Log-Off-Time.xls contents are,
7/11/2013 6:49:56 PM
7/15/2013 9:08:22 PM
7/16/2013 9:09:56 PM
7/17/2013 9:12:45 PM
7/18/2013 6:09:08 PM
7/19/2013 7:37:08 PM
7/22/2013 8:51:30 PM
7/23/2013 9:30:05 PM
7/24/2013 9:33:08 PM
7/25/2013 8:31:17 PM
7/26/2013 8:06:36 PM
Finally got the answer...
Just added the code below.
Dim Diff As Date
Diff = CDate(OffTime) - CDate(InTime)
Find the complete code below.
Sub Average()
Dim LogIn As String
Dim LogOff As String
Dim Diff As Date
Dim Row As Integer
Dim Col As Integer
Dim InTime As Date
Dim OffTime As Date
Row = 1
Col = 2
While (Cells(Row, Col) <> "")
Workbooks("Log-In-Time.xlsm").Activate
InTime = Cells(Row, Col)
Workbooks("Log-Off-Time.xlsm").Activate
OffTime = Cells(Row, Col)
Workbooks("Log-In-Time.xlsm").Activate
Diff = CDate(OffTime) - CDate(InTime)
Cells(Row, Col + 1) = Diff
Row = Row + 1
Wend
End Sub
I am using this code for display dates from current date to next six days.
if any other code for display date like this. please help
Private Sub Displaydate()
cn.ConnectionString = System.Configuration.ConfigurationManager.AppSettings("DataConnectionString")
lblDateday.Text = System.DateTime.Now.ToString("dddd")
lblMonthdate.Text = System.DateTime.Now.ToString("dd MMMM ")
lblDateday2.Text = System.DateTime.Now.AddDays(1).ToString("dddd")
lblMonthdate2.Text = System.DateTime.Now.AddDays(1).ToString("dd MMMM ")
lblDateday3.Text = System.DateTime.Now.AddDays(2).ToString("dddd")
lblMonthdate3.Text = System.DateTime.Now.AddDays(2).ToString("dd MMMM ")
lblDateday4.Text = System.DateTime.Now.AddDays(3).ToString("dddd")
lblMonthdate4.Text = System.DateTime.Now.AddDays(3).ToString("dd MMMM ")
lblDateday5.Text = System.DateTime.Now.AddDays(4).ToString("dddd")
lblMonthdate5.Text = System.DateTime.Now.AddDays(4).ToString("dd MMMM ")
lblDateday6.Text = System.DateTime.Now.AddDays(5).ToString("dddd")
lblMonthdate6.Text = System.DateTime.Now.AddDays(5).ToString("dd MMMM ")
lblDateday7.Text = System.DateTime.Now.AddDays(6).ToString("dddd")
lblMonthdate7.Text = System.DateTime.Now.AddDays(6).ToString("dd MMMM ")
End Sub
the output is
Wednesday Thursday Friday Saturday Sunday Monday Tuesday
21 November 22 November 23 November 24 November 25 November 26 November 27November
Put the things you handle into arrays and then do loops instead.
Assign Lists globally
Dim DateDayList as List(of Label) = new List(of Label)
Dim MonthDayList as List(of Label) = new List(of Label)
Add all dateDay labels to list in the correct order inside the Initialize sub.
DateDayList.Add(lblDateDay)
DateDayList.Add(lblDateDay2)
etc.
Do the same with the Month day labels.
Then simply do this:
for i as Integer = 0 To 6
DateDayList(i).Text = System.DateTime.Now.AddDays(i).ToString("dddd")
MonthDayList(i).Text = System.DateTime.Now.AddDays(i).ToString("dd MMMM ")
next
I have a two Input data of Year and Name in separate two arrays. I need to sort both the array values first i need to sort it chronologically(Year) and then if year information repeats it will sort the Array Alphabetically.
As for as I complete the sorting for both year and then name. Using Wordbasic.sortarray command
Input: (Before sorting)
SDF 1997
ELS 1986
PJK 1983
WKL 1995
EFD 1986
Output: (After sorting)
PJK 1983
EFD 1986
ELS 1986
WKL 1995
SDF 1997
If I print it in word it printed like this:
PJK 1983, ELS 1986, EFD 1986, WKL 1995, SDF 1997.
Here is my code for Printing the data. Would anyone please look into this and guide me where did i made mistake?
WordBasic.sortarray SortyearArray()
Code:
Dim I As Integer
Dim J As Integer
Dim K As Integer
Dim N As Integer
Dim Counter As Integer
COUNTER1 = 1
i1 = 1
J1 = 5
For I = 0 To UBound(SortyearArray())
Counter = 1
For J = I + 1 To UBound(SortyearArray())
If SortyearArray(I) = SortyearArray(J) Then
Counter = Counter + 1
MsgBox (Counter)
End If
COUNTER1 = Counter + COUNTER1
Next J
If Counter = 1 Then
For N = i1 To J1
If SortyearArray(I) = Year(N) Then
Selection.TypeText Text:="(" & AuthorName(N) & Year(N) & ")"
End If
Next N
End If
Next I
The input
SDF 1997
ELS 1986
PJK 1983
WKL 1995
EFD 1986
Core functions:
Public Function QuickSort(ByRef array2check() As String, min As Long, max As Long) As Boolean
Dim lo As Long, hi As Long
Dim lo0 As Long, hi0 As Long
Dim midPos As String
lo = min: hi = max
lo0 = lo: hi0 = hi
midPos = array2check((lo0 + hi0) / 2)
DoEvents
While (lo <= hi)
While ((lo < hi0) And (array2check(lo) < midPos))
lo = lo + 1
Wend
While ((hi > lo0) And (array2check(hi) > midPos))
hi = hi - 1
Wend
If lo <= hi Then
Call swap(array2check, lo, hi)
lo = lo + 1
hi = hi - 1
End If
DoEvents
Wend
If lo0 < hi Then Call QuickSort(array2check, lo0, hi)
If lo < hi0 Then Call QuickSort(array2check, lo, hi0)
QuickSort = True
End Function
Private Sub swap(arr() As String, idx1 As Long, idx2 As Long)
Dim tmp As String
tmp = arr(idx1)
arr(idx1) = arr(idx2)
arr(idx2) = tmp
End Sub
The Sample tester
Public Sub sample_test()
Dim test_arr() As String
test_arr = Split("SDF 1997" & vbCrLf & "ELS 1986" & vbCrLf & "PJK 1983" & vbCrLf & "WKL 1995" & vbCrLf & "EFD 1986", vbCrLf)
If QuickSort(test_arr, LBound(test_arr), UBound(test_arr)) = True Then
'Debug.Print Join(test_arr, vbCrLf)
MsgBox Join(test_arr, vbCrLf)
End If
End Sub
The Result
EFD 1986
ELS 1986
PJK 1983
SDF 1997
WKL 1995
Hope this helps.