10 Replies - 3472 Views - Last Post: 03 February 2012 - 04:53 AM Rate Topic: -----

#1 Msqueen082  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 06-February 11

Problem converting normal dates to julian date

Posted 03 April 2011 - 02:12 PM

hi again..
Could sure use some help in another code I am currently working on. Would really appreciate the help.
Today i am trying to convert a date i put in a textbox(txtJulian) to a normal date like 02/05/2011(or any other conversion depending what is imputed in the textbox). I cant get it to print to correct date. When I type 011-2011 it print out 1/11/2000when I want it to print 1/11/2011. Also the error messag doesnt show when I type nothing or the correct format in the txt box. Here is my code
Private Sub btnConvert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConvert.Click

        'holds the value of what the user inputs in the textbox
        Dim JulianDate As Long

        Dim year As Long
        Dim ConvertToJulianDate As Date

        If txtJulian.Text = "" Or txtJulian.Text = Format("DDD - YYY") Then
            lblOut.Text = "Julian date must have a dash"
        End If

        'Prompts the user to input in textbox and assign the value entered to then JulianDate 
        JulianDate = Val(txtJulian.Text)

        'converts julian date to a regular date
        If Int(JulianDate / 1000) < 30 Then
            year += 2000
        Else
            year += 1900
        End If

        ConvertToJulianDate = DateSerial(year + Int(JulianDate / 1000), _
        1, JulianDate Mod 1000)

        'Display the new date in the normal date format.
        lblOut.Text = ConvertToJulianDate
        
    End Sub
End Class


Thank you :angel:

Is This A Good Question/Topic? 0
  • +

Replies To: Problem converting normal dates to julian date

#2 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1046
  • View blog
  • Posts: 4,072
  • Joined: 02-July 08

Re: Problem converting normal dates to julian date

Posted 03 April 2011 - 07:05 PM

I think I just simplified what your doing.

Dim julian_date As String = "011-2011"
Dim julianDatePart() As String = julian_date.Split("-"c)
Dim dt As New Date(CInt(julianDatePart(1)), 1, 1)
Dim returnDate As Date = dt.AddDays(CInt(julianDatePart(0)) - 1)
MessageBox.Show(returnDate.ToString)

Was This Post Helpful? 1
  • +
  • -

#3 Msqueen082  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 06-February 11

Re: Problem converting normal dates to julian date

Posted 03 April 2011 - 07:19 PM

Thank you it worked, but can you explain what each line does so that I may understand it better..would really appreciated it.

:angel:
Was This Post Helpful? 0
  • +
  • -

#4 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1046
  • View blog
  • Posts: 4,072
  • Joined: 02-July 08

Re: Problem converting normal dates to julian date

Posted 03 April 2011 - 07:40 PM

String that holds your julian date, could be your textbox,
Dim julian_date As String = "011-2011"

String array to simplify which part I want
julianDatePart(0) = 011
julianDatePart(1) = 2011
it's just spliting on the delimiter "-"
Dim julianDatePart() As String = julian_date.Split("-"c)

Create a new date using the year and just set the month and day to 1
Dim dt As New Date(CInt(julianDatePart(1)), 1, 1)

Now lets add the days, we have to minus 1 because we started on the first of the month,
Dim returnDate As Date = dt.AddDays(CInt(julianDatePart(0)) - 1)

Return the value,
MessageBox.Show(returnDate.ToString)


Also I would just make a function out of this,

Private Function ConvertJulianDate(ByVal julian_date As String) As Date 
  Dim julianDatePart() As String = julian_date.Split("-"c)
  Dim dt As New Date(CInt(julianDatePart(1)), 1, 1)
  Return dt.AddDays(CInt(julianDatePart(0)) - 1)
End Function

This post has been edited by hawkvalley1: 03 April 2011 - 07:42 PM

Was This Post Helpful? 1
  • +
  • -

#5 dbasnett  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 109
  • View blog
  • Posts: 603
  • Joined: 01-October 08

Re: Problem converting normal dates to julian date

Posted 04 April 2011 - 04:55 AM

Some old code that works with Julian dates...

    '2299160.5 - Oct 15, 1582 was the day the calendars changed
    'the followinng two routines do not account for the missing 10 days

    Public Function toJulian(ByVal aDate As DateTime) As Double 'returns the date as a double=julian date
        'toJulian - converts datetime to julian date
        'VB .Net implementation of:
        '   - http://www.astro.uu.nl/~strous/AA/en/reken/juliaansedag.html
        'with time code added

        'aDate is assumed to be UTC datetime <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

        Dim c, d, m, j, JulianDate As Double
        'encode time
        Dim t As Double = ((aDate.Hour * 60 * 60) + (aDate.Minute * 60) + (aDate.Second)) / (24 * 60 * 60)
        d = aDate.Day + t 'the day portion = the day + the time as calculated above
        m = aDate.Month
        j = aDate.Year
        If m < 3 Then
            m += 12
            j -= 1
        End If
        c = 2 - Math.Floor(j / 100) + Math.Floor(j / 400)
        JulianDate = Math.Floor(1461 * (j + 4716) / 4) + Math.Floor(153 * (m + 1) / 5) + d + c - 1524.5
        Return JulianDate
    End Function

    Public Function fromJulian(ByVal JDt As Double) As DateTime
        'fromJulian - returns the date based on a julian date
        'VB .Net implementation of:
        'based on - http://www.astro.uu.nl/~strous/AA/en/reken/juliaansedag.html
        'with time code added

        Dim RetVal As DateTime
        Dim d, e, h, i, j, m, n, p, q, s1, s2, s3, s4 As Double
        j = JDt
        p = Math.Floor(j + 0.5)
        s1 = p + 68569
        n = Math.Floor(4 * s1 / 146097)
        s2 = s1 - Math.Floor((146097 * n + 3) / 4)
        i = Math.Floor(4000 * (s2 + 1) / 1461001)
        s3 = s2 - Math.Floor(1461 * i / 4) + 31
        q = Math.Floor(80 * s3 / 2447)
        e = s3 - Math.Floor(2447 * q / 80)
        s4 = Math.Floor(q / 11)
        m = q + 2 - 12 * s4
        j = 100 * (n - 49) + i + s4
        d = e + JDt - p + 0.5
        h = d - (Math.Floor(d))
        d = d - h
        RetVal = DateSerial(CInt(j), CInt(m), CInt(d))
        RetVal = RetVal.AddDays(h)
        Return (RetVal)
    End Function


Was This Post Helpful? 1
  • +
  • -

#6 Msqueen082  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 06-February 11

Re: Problem converting normal dates to julian date

Posted 04 April 2011 - 09:38 AM

Thank you both so much...it was very helpful

Thank you hawkvalley for the explaining the code..was very helpful in understanding it ..

:angel:
Was This Post Helpful? 0
  • +
  • -

#7 Msqueen082  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 06-February 11

Re: Problem converting normal dates to julian date

Posted 04 April 2011 - 06:53 PM

I have one more dilemma with this code before I wrap it up...

in the code I have to add several error messages..I got 2 to work but I cant seem to get the rest to work. the error code are

1)day part must be numeric
2)day cannot exceed 365 days or less or equal to 1

3)Year part must be numeric
4)year must be 4 digits

I'm thinking in the array where the deliminator (-) add indexes that hold
year and day..then I can use them in the error messages.

Here is what I have so far..everything works except me adding those error messages.

Private Sub btnConvert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConvert.Click

        Dim julianDate As String
        Dim convertJulianDate() As String
        Dim returnDate As Date

        If txtJulian.Text = "" Or txtJulian.Text.IndexOf("-") = -1 Then
            lblOut.Text = "Julian date must have a dash"
            txtJulian.Focus()
            txtJulian.SelectAll()
            Exit Sub
        End If

        julianDate = txtJulian.Text

        convertJulianDate = julianDate.Split("-"c)

        Dim dt As New Date(CInt(convertJulianDate(1)), 1, 1)

        returnDate = dt.AddDays(CInt(convertJulianDate(0)) - 1)

        lblOut.Text = returnDate.ToShortDateString
    End Sub
End Class



thank you
:angel:
Was This Post Helpful? 0
  • +
  • -

#8 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1046
  • View blog
  • Posts: 4,072
  • Joined: 02-July 08

Re: Problem converting normal dates to julian date

Posted 04 April 2011 - 07:37 PM

Well I would use Regex to make the button enabled or disabled based on the text it holds.

In the textChanged event for the textbox where they enter the data you can validate for proper format. This would enable the button so they can perform the conversion, but only if the format is correct. The expression can be refined further to capture a range.

btnConvert.Enabled = Regex.IsMatch(tb.Text, "\d{3}-\d{4}")


Which requires the Imports System.Text.RegularExpressions namespace.

This post has been edited by hawkvalley1: 04 April 2011 - 07:50 PM

Was This Post Helpful? 1
  • +
  • -

#9 Msqueen082  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 06-February 11

Re: Problem converting normal dates to julian date

Posted 05 April 2011 - 07:39 PM

Sorry, but I've never used Regex...I am a beginner and just want to show the error message in a label on the form. I am using a sub and not a function to display my code. I've got the errors to show, but that is not what professor wants...to show error messages, like if they use any letter or number for the day (4Y-2000) than it should should show and error for the day part which is the day part of what is typed in txtbox..here is the the client should type (ddd-yyyy). Here is my code
Private Sub btnConvert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConvert.Click

        Dim julianDate As String
        Dim convertJulianDate() As String
        Dim returnDate As Date
        Dim noDash As String = "3222010"
        Dim invalidYear As String = "322-20XX"
        Dim dy As String = "378-2011"
        Dim FourDigitYear As String = "322-09"
        Dim dayNumeric As String = "4Y-2000"

        If txtJulian.Text = "" Then
            lblOut.Text = "Must enter a Julian Date"
            txtJulian.Focus()
            txtJulian.SelectAll()
            Exit Sub
        End If

        If txtJulian.Text = noDash Then
            lblOut.Text = "Julian date must have a dash"
            txtJulian.Focus()
            txtJulian.SelectAll()
            Exit Sub
        End If

        If txtJulian.Text = invalidYear Then
            lblOut.Text = "Julian year not numeric"
            txtJulian.Focus()
            txtJulian.SelectAll()
            Exit Sub
        End If

        If txtJulian.Text = dy Then
            lblOut.Text = "Julian days out of range"
            txtJulian.Focus()
            txtJulian.SelectAll()
            Exit Sub
        End If

        If txtJulian.Text = FourDigitYear Then
            lblOut.Text = "Julian year must be four digits"
            txtJulian.Focus()
            txtJulian.SelectAll()
            Exit Sub
        End If

        If txtJulian.Text = dayNumeric Then
            lblOut.Text = "Julian Day not numeric"
            txtJulian.Focus()
            txtJulian.SelectAll()
            Exit Sub
        End If
        'holds the julian date in txtJulian textbox.
        julianDate = txtJulian.Text

        'spliting on the delimiter "-"
        convertJulianDate = julianDate.Split("-"c)

        'Created a new date using the year and set the month and day to 1.
        Dim dt As New Date(CInt(convertJulianDate(1)), 1, 1)

        'Add the days, and minus 1 because I started on the first of the month
        returnDate = dt.AddDays(CInt(convertJulianDate(0)) - 1)

        'prints out the return date in the label
        lblOut.Text = returnDate.ToShortDateString
    End Sub
End Class


thank you all for your help
greatly appreciated
:angel:
Was This Post Helpful? 0
  • +
  • -

#10 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1046
  • View blog
  • Posts: 4,072
  • Joined: 02-July 08

Re: Problem converting normal dates to julian date

Posted 05 April 2011 - 07:56 PM

Regex is such a rare tool that I don't mind designing a pattern for you. Still it is worthy of learning as it can be a valuable tool. Here is the basic code design I see:

If Regex.IsMatch(tb.Text, "\d{3}-\d{4}") Then
   'process the string
Else
   'error message
End If

Was This Post Helpful? 1
  • +
  • -

#11 Asokraja  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 03-February 12

Re: Problem converting normal dates to julian date

Posted 03 February 2012 - 04:53 AM

View Post_HAWK_, on 03 April 2011 - 07:05 PM, said:

I think I just simplified what your doing.

Dim julian_date As String = "011-2011"
Dim julianDatePart() As String = julian_date.Split("-"c)
Dim dt As New Date(CInt(julianDatePart(1)), 1, 1)
Dim returnDate As Date = dt.AddDays(CInt(julianDatePart(0)) - 1)
MessageBox.Show(returnDate.ToString)

Hello Mr.Hawk ., i am looking forward to convert a group of date in normal date in Excel sheet(.csv) to Julian date where i need the o/p also in .csv
Thanking you . Please do help me with this i am coding the application in VB.NET
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1