incrementing dates in a text file

nicknack

New member
Joined
Aug 22, 2016
Messages
4
Programming Experience
Beginner
I would like to print out on text file a range of dates-times that increment from 1 point in date-time to another by 1 second. 1977-08-11T07:30:43.000z to 1977-08-11T07:30:51.000z

Can i do that in vb.net without reading the last line of the text file before printing out the next line as i create the increments?
I have limited vb.net experience. Someone told me I had to read the last line as I generate the list. I desire a list that looks like this.

1977-08-11T07:30:43.000z
1977-08-11T07:30:44.000z
1977-08-11T07:30:45.000z
1977-08-11T07:30:46.000z
1977-08-11T07:30:47.000z
1977-08-11T07:30:48.000z
1977-08-11T07:30:49.000z
1977-08-11T07:30:50.000z
1977-08-11T07:30:51.000z

If i have to read the last line, my concern is being able to read the last line and converting it back into date that vb.net knows as a date not just a string.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,602
Location
Sydney, Australia
Programming Experience
10+
Are you saying that you want to create a new file or that you want to append to an existing file? If it's the former then you absolutely don't have to read anything from the file but if it's the latter then you do. With regards to converting a String to a Date, you have multiple options and the best is dependent on the circumstances. You have CDate, Convert.ToDateTime, Date.Parse, Date.TryParse, Date.ParseExact and Date.TryParseExact.
 

nicknack

New member
Joined
Aug 22, 2016
Messages
4
Programming Experience
Beginner
Maybe you have a clue to what my problem is

my text file looks like this
1977-23-23T09:12:12.000z
1977-23-23T09:22:22.000z
1977-23-23T09:44:44.000z

my program so far looks like this;
Dim Findstring = System.IO.File.ReadAllLines("C:\VALUE-Contains-Date.txt").Last ' reads the last line in text file 'works
Dim dateFormat As String = "yyyy-MM-dd HH:mm:ss.fff" 'works
Dim capture As String = Findstring.Substring(0, 23) ' captures string 23 characters 'works
MsgBox("captured " & capture) 'works shows 1977-23-23T09:44:44.000 which is last item on the list in message box
'------dont change above this. so at this point i know i have as a string the last date

'Dim time = DateTime.Parse(capture) 'not working
MsgBox("time " & time) 'not working message never shows up
why would this last line "Dim time = DateTime.Parse(capture)" not work


i used the first example found here to guide me
http://www.dotnetperls.com/datetime-parse-vbnet
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,602
Location
Sydney, Australia
Programming Experience
10+
Firstly, you should be calling ReadLines in that scenario rather than ReadAllLines. It's not a big deal for a small file but ReadAllLines creates an array containing every line whereas ReadLines reads each line one by one and discards the previous each time.

As for why Date.Parse wouldn't work, It's because it has to be a format that the method understands. On this case, it's more appropriate call ParseExact and then provide the exact format.
 

nicknack

New member
Joined
Aug 22, 2016
Messages
4
Programming Experience
Beginner
thank you. those ideas sent me on a better path.
my text file now looks like this
2010-02-26T08:37:29.0904778Z
2011-02-26T08:37:31.0904778Z
2012-02-26T08:37:35.0904778Z

my code looks like this
----------------------------------------------------------------------------
Imports System.Globalization

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim enUS As New CultureInfo("en-US")
        Dim dateString As String
        Dim dateValue As Date
        Dim Findstring = System.IO.File.ReadLines("C:\VALUE-Contains-Date2.txt").Last  ' reads the last line in text file 'works

        ' Parse a string represengting UTC.

        'dateString = Findstring.Substring(0, 28)               'if i use this line for dateString it wont work.   A


        dateString = "2008-06-11T16:11:20.0904778Z"       'if i use this line it works meaning it adds 1 second and shows it in message box.But its not reading the text file anymore  B


        dateValue = Date.ParseExact(dateString, "o", CultureInfo.InvariantCulture, _
                                    DateTimeStyles.None)
        MsgBox("Time before You add Second " & dateValue)
        Dim timeyouwanttoadd As Date = dateValue.AddSeconds(1)
        MsgBox("Time After You add Second " & timeyouwanttoadd)



    End Sub
End Class

----------------------------------------------------------------------------
When i use line A it doesnt work anymore
When i use line B program works but its not using the last line from the text file its just using the 2008 date
Whats the difference? Why would Line A not work the same way as if i used line B but just use the last line of text file?
 
Last edited by a moderator:

nicknack

New member
Joined
Aug 22, 2016
Messages
4
Programming Experience
Beginner
Iam using substring because that ability will help me fine the date on the line when each line is filled with other information. It also allows me to find and use chars within the date which will be used for convertion to a julean date-time which i already worked out. Ialso need the characters in the date to to help me create a while loop based on the month. Those are the things I already know how to program using substring. They go beyond the scope of this question though. I would think since line A and Line B are both strings they should both work if they look the same.
 
Top Bottom