What is the function used to get the current time updated ??

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rhepsi
    New Member
    • Apr 2007
    • 111

    What is the function used to get the current time updated ??

    Hii all,

    im doin a vb project where i need to update the current date..??


    Code:
    (vb)
    
      Private Function updateDate(ByVal strDate As String) As String
            'to convert valid date format
            Dim xdate As String
            Try
                xdate = "'" + Format(Convert.ToDateTime(strDate), "yyyy-MM-dd") + "'"
            Catch ex As Exception
                xdate = "'" + "0000-00-00" + "'"
            End Try
            updateDate = xdate
            Return updateDate
        End Function
    plz someone tell me wat changes i need to do to the code to get the current time....

    thnq..
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    What exactly are you trying to do? You keep using contradictory terms.

    Getting the current date and time is as easy as:
    Code:
    DateTime rightnow= DateTime.Now;
    String mydatestring = rightnow.ToString();
    // .ToString() can take a number of different arguments that determine how and what gets displayed
    Setting the date and time for the computer that the software is running on is something that I don't know a good way to do.
    You can run a proccess for command line commands: TIME and DATE
    (open up a command prompt and type "help date" and "help time" to see more about them)
    But there is probably a better way.

    Comment

    • SammyB
      Recognized Expert Contributor
      • Mar 2007
      • 807

      #3
      Originally posted by rhepsi
      Hii all,

      im doin a vb project where i need to update the current date..??


      Code:
      (vb)
       
      Private Function updateDate(ByVal strDate As String) As String
      'to convert valid date format
      Dim xdate As String
      Try
      xdate = "'" + Format(Convert.ToDateTime(strDate), "yyyy-MM-dd") + "'"
      Catch ex As Exception
      xdate = "'" + "0000-00-00" + "'"
      End Try
      updateDate = xdate
      Return updateDate
      End Function
      plz someone tell me wat changes i need to do to the code to get the current time....

      thnq..
      If you want to display the date and/or time as a string, then just use the FormatDateTime function.

      Comment

      • rhepsi
        New Member
        • Apr 2007
        • 111

        #4
        Originally posted by Plater
        What exactly are you trying to do? You keep using contradictory terms.

        Getting the current date and time is as easy as:
        Code:
        DateTime rightnow= DateTime.Now;
        String mydatestring = rightnow.ToString();
        // .ToString() can take a number of different arguments that determine how and what gets displayed
        Setting the date and time for the computer that the software is running on is something that I don't know a good way to do.
        You can run a proccess for command line commands: TIME and DATE
        (open up a command prompt and type "help date" and "help time" to see more about them)
        But there is probably a better way.
        Hey sorry i want the current date in the format of "yyyy-MM-dd"
        i have a function here:

        {
        Code:
        vb}
        
         Private Function updateDate(ByVal strDate As String) As String
                'to convert valid date format
                Dim xdate As String
                Try
        .....>>   xdate = "'" + Format(Convert.ToDateTime(strDate), "yyyy-MM-dd") + "'"....>>>     <<< doubt>>>
        
         Catch ex As Exception
                    xdate = "'" + "0000-00-00" + "'"
                End Try
                updateDate = xdate
                Return updateDate
            End Function

        When im trying to execute the program im not getting the curent date but instead im getting null values as in the catch .....

        Wat i need to do to get the actual or the current date........... ..???

        thnx in advance

        Comment

        • dskinibbyb
          New Member
          • Mar 2007
          • 8

          #5
          Originally posted by rhepsi
          Hey sorry i want the current date in the format of "yyyy-MM-dd"
          i have a function here:

          {
          Code:
          vb}
          
           Private Function updateDate(ByVal strDate As String) As String
                  'to convert valid date format
                  Dim xdate As String
                  Try
          .....>>   xdate = "'" + Format(Convert.ToDateTime(strDate), "yyyy-MM-dd") + "'"....>>>     <<< doubt>>>
          
           Catch ex As Exception
                      xdate = "'" + "0000-00-00" + "'"
                  End Try
                  updateDate = xdate
                  Return updateDate
              End Function

          When im trying to execute the program im not getting the curent date but instead im getting null values as in the catch .....

          Wat i need to do to get the actual or the current date........... ..???

          thnx in advance
          Hi,
          instead of anothe function u can do it in one single line as below,
          Dim str As String
          str = Format(CDate((F ormatDateTime(S ystem.DateTime. Now, DateFormat.Gene ralDate))), "yyyy-MM-dd")
          And i tried ur code and its working fine and its not going into catch section.
          even though if you are getting the error try to debug and see where its failing..

          Comment

          • rhepsi
            New Member
            • Apr 2007
            • 111

            #6
            Originally posted by dskinibbyb
            Hi,
            instead of anothe function u can do it in one single line as below,
            Dim str As String
            str = Format(CDate((F ormatDateTime(S ystem.DateTime. Now, DateFormat.Gene ralDate))), "yyyy-MM-dd")
            And i tried ur code and its working fine and its not going into catch section.
            even though if you are getting the error try to debug and see where its failing..

            Thanq so much... its working nw....

            Comment

            • rhepsi
              New Member
              • Apr 2007
              • 111

              #7
              Originally posted by dskinibbyb
              Hi,
              instead of anothe function u can do it in one single line as below,
              Dim str As String
              str = Format(CDate((F ormatDateTime(S ystem.DateTime. Now, DateFormat.Gene ralDate))), "yyyy-MM-dd")
              And i tried ur code and its working fine and its not going into catch section.
              even though if you are getting the error try to debug and see where its failing..

              Hii,

              if im gonna update the existing date then... wat should be the difference..??

              Comment

              • Plater
                Recognized Expert Expert
                • Apr 2007
                • 7872

                #8
                Originally posted by rhepsi
                Hey sorry i want the current date in the format of "yyyy-MM-dd"
                You guys made that WAY to over complex
                Code:
                string mydate=DateTime.Now.ToString("yyyy-MM-dd");
                This is from the msdn help on DateTime.ToStri ng(String)-> DateTimeFormatI nfo section:
                [html]
                Format Pattern
                Description

                d, %d
                The day of the month. Single-digit days will not have a leading zero. Specify "%d" if the format pattern is not combined with other format patterns.

                dd
                The day of the month. Single-digit days will have a leading zero.

                ddd
                The abbreviated name of the day of the week, as defined in AbbreviatedDayN ames.

                dddd
                The full name of the day of the week, as defined in DayNames.

                M, %M
                The numeric month. Single-digit months will not have a leading zero. Specify "%M" if the format pattern is not combined with other format patterns.

                MM
                The numeric month. Single-digit months will have a leading zero.

                MMM
                The abbreviated name of the month, as defined in AbbreviatedMont hNames.

                MMMM
                The full name of the month, as defined in MonthNames.

                y, %y
                The year without the century. If the year without the century is less than 10, the year is displayed with no leading zero. Specify "%y" if the format pattern is not combined with other format patterns.

                yy
                The year without the century. If the year without the century is less than 10, the year is displayed with a leading zero.

                yyyy
                The year in four digits, including the century.

                gg
                The period or era. This pattern is ignored if the date to be formatted does not have an associated period or era string.

                h, %h
                The hour in a 12-hour clock. Single-digit hours will not have a leading zero. Specify "%h" if the format pattern is not combined with other format patterns.

                hh
                The hour in a 12-hour clock. Single-digit hours will have a leading zero.

                H, %H
                The hour in a 24-hour clock. Single-digit hours will not have a leading zero. Specify "%H" if the format pattern is not combined with other format patterns.

                HH
                The hour in a 24-hour clock. Single-digit hours will have a leading zero.

                m, %m
                The minute. Single-digit minutes will not have a leading zero. Specify "%m" if the format pattern is not combined with other format patterns.

                mm
                The minute. Single-digit minutes will have a leading zero.

                s, %s
                The second. Single-digit seconds will not have a leading zero. Specify "%s" if the format pattern is not combined with other format patterns.

                ss
                The second. Single-digit seconds will have a leading zero.

                f, %f
                The fraction of a second in single-digit precision. The remaining digits are truncated. Specify "%f" if the format pattern is not combined with other format patterns.

                ff
                The fraction of a second in double-digit precision. The remaining digits are truncated.

                fff
                The fraction of a second in three-digit precision. The remaining digits are truncated.

                ffff
                The fraction of a second in four-digit precision. The remaining digits are truncated.

                fffff
                The fraction of a second in five-digit precision. The remaining digits are truncated.

                ffffff
                The fraction of a second in six-digit precision. The remaining digits are truncated.

                fffffff
                The fraction of a second in seven-digit precision. The remaining digits are truncated.

                F, %F
                Displays the most significant digit of the seconds fraction. Nothing is displayed if the digit is zero. Specify "%F" if the format pattern is not combined with other format patterns.

                FF
                Displays the two most significant digits of the seconds fraction. However, trailing zeros, or two zero digits, are not displayed.

                FFF
                Displays the three most significant digits of the seconds fraction. However, trailing zeros, or three zero digits, are not displayed.

                FFFF
                Displays the four most significant digits of the seconds fraction. However, trailing zeros, or four zero digits, are not displayed.

                FFFFF
                Displays the five most significant digits of the seconds fraction. However, trailing zeros, or five zero digits, are not displayed.

                FFFFFF
                Displays the six most significant digits of the seconds fraction. However, trailing zeros, or six zero digits, are not displayed.

                FFFFFFF
                Displays the seven most significant digits of the seconds fraction. However, trailing zeros, or seven zero digits, are not displayed.

                t, %t
                The first character in the AM/PM designator defined in AMDesignator or PMDesignator, if any. Specify "%t" if the format pattern is not combined with other format patterns.

                tt
                The AM/PM designator defined in AMDesignator or PMDesignator, if any.

                z, %z
                The time zone offset ("+" or "-" followed by the hour only). Single-digit hours will not have a leading zero. For example, Pacific Standard Time is "-8". Specify "%z" if the format pattern is not combined with other format patterns.

                zz
                The time zone offset ("+" or "-" followed by the hour only). Single-digit hours will have a leading zero. For example, Pacific Standard Time is "-08".

                zzz
                The full time zone offset ("+" or "-" followed by the hour and minutes). Single-digit hours and minutes will have leading zeros. For example, Pacific Standard Time is "-08:00".

                :
                The default time separator defined in TimeSeparator.

                /
                The default date separator defined in DateSeparator.

                % c
                Where c is a format pattern if used alone. That is, to use format pattern "d", "f", "F", "h", "m", "s", "t", "y", "z", "H", or "M" by itself, specify "%d", "%f", "%F", "%h", "%m", "%s", "%t", "%y", "%z", "%H", or "%M".

                The "%" character can be omitted if the format pattern is combined with literal characters or other format patterns.

                \ c
                Where c is any character. Displays the character literally. To display the backslash character, use "\\".

                [/html]

                Comment

                • rhepsi
                  New Member
                  • Apr 2007
                  • 111

                  #9
                  Originally posted by Plater
                  You guys made that WAY to over complex
                  Code:
                  string mydate=DateTime.Now.ToString("yyyy-MM-dd");
                  This is from the msdn help on DateTime.ToStri ng(String)-> DateTimeFormatI nfo section:
                  [html]
                  Format Pattern
                  Description

                  d, %d
                  The day of the month. Single-digit days will not have a leading zero. Specify "%d" if the format pattern is not combined with other format patterns.

                  dd
                  The day of the month. Single-digit days will have a leading zero.

                  ddd
                  The abbreviated name of the day of the week, as defined in AbbreviatedDayN ames.

                  dddd
                  The full name of the day of the week, as defined in DayNames.

                  M, %M
                  The numeric month. Single-digit months will not have a leading zero. Specify "%M" if the format pattern is not combined with other format patterns.

                  MM
                  The numeric month. Single-digit months will have a leading zero.

                  MMM
                  The abbreviated name of the month, as defined in AbbreviatedMont hNames.

                  MMMM
                  The full name of the month, as defined in MonthNames.

                  y, %y
                  The year without the century. If the year without the century is less than 10, the year is displayed with no leading zero. Specify "%y" if the format pattern is not combined with other format patterns.

                  yy
                  The year without the century. If the year without the century is less than 10, the year is displayed with a leading zero.

                  yyyy
                  The year in four digits, including the century.

                  gg
                  The period or era. This pattern is ignored if the date to be formatted does not have an associated period or era string.

                  h, %h
                  The hour in a 12-hour clock. Single-digit hours will not have a leading zero. Specify "%h" if the format pattern is not combined with other format patterns.

                  hh
                  The hour in a 12-hour clock. Single-digit hours will have a leading zero.

                  H, %H
                  The hour in a 24-hour clock. Single-digit hours will not have a leading zero. Specify "%H" if the format pattern is not combined with other format patterns.

                  HH
                  The hour in a 24-hour clock. Single-digit hours will have a leading zero.

                  m, %m
                  The minute. Single-digit minutes will not have a leading zero. Specify "%m" if the format pattern is not combined with other format patterns.

                  mm
                  The minute. Single-digit minutes will have a leading zero.

                  s, %s
                  The second. Single-digit seconds will not have a leading zero. Specify "%s" if the format pattern is not combined with other format patterns.

                  ss
                  The second. Single-digit seconds will have a leading zero.

                  f, %f
                  The fraction of a second in single-digit precision. The remaining digits are truncated. Specify "%f" if the format pattern is not combined with other format patterns.

                  ff
                  The fraction of a second in double-digit precision. The remaining digits are truncated.

                  fff
                  The fraction of a second in three-digit precision. The remaining digits are truncated.

                  ffff
                  The fraction of a second in four-digit precision. The remaining digits are truncated.

                  fffff
                  The fraction of a second in five-digit precision. The remaining digits are truncated.

                  ffffff
                  The fraction of a second in six-digit precision. The remaining digits are truncated.

                  fffffff
                  The fraction of a second in seven-digit precision. The remaining digits are truncated.

                  F, %F
                  Displays the most significant digit of the seconds fraction. Nothing is displayed if the digit is zero. Specify "%F" if the format pattern is not combined with other format patterns.

                  FF
                  Displays the two most significant digits of the seconds fraction. However, trailing zeros, or two zero digits, are not displayed.

                  FFF
                  Displays the three most significant digits of the seconds fraction. However, trailing zeros, or three zero digits, are not displayed.

                  FFFF
                  Displays the four most significant digits of the seconds fraction. However, trailing zeros, or four zero digits, are not displayed.

                  FFFFF
                  Displays the five most significant digits of the seconds fraction. However, trailing zeros, or five zero digits, are not displayed.

                  FFFFFF
                  Displays the six most significant digits of the seconds fraction. However, trailing zeros, or six zero digits, are not displayed.

                  FFFFFFF
                  Displays the seven most significant digits of the seconds fraction. However, trailing zeros, or seven zero digits, are not displayed.

                  t, %t
                  The first character in the AM/PM designator defined in AMDesignator or PMDesignator, if any. Specify "%t" if the format pattern is not combined with other format patterns.

                  tt
                  The AM/PM designator defined in AMDesignator or PMDesignator, if any.

                  z, %z
                  The time zone offset ("+" or "-" followed by the hour only). Single-digit hours will not have a leading zero. For example, Pacific Standard Time is "-8". Specify "%z" if the format pattern is not combined with other format patterns.

                  zz
                  The time zone offset ("+" or "-" followed by the hour only). Single-digit hours will have a leading zero. For example, Pacific Standard Time is "-08".

                  zzz
                  The full time zone offset ("+" or "-" followed by the hour and minutes). Single-digit hours and minutes will have leading zeros. For example, Pacific Standard Time is "-08:00".

                  :
                  The default time separator defined in TimeSeparator.

                  /
                  The default date separator defined in DateSeparator.

                  % c
                  Where c is a format pattern if used alone. That is, to use format pattern "d", "f", "F", "h", "m", "s", "t", "y", "z", "H", or "M" by itself, specify "%d", "%f", "%F", "%h", "%m", "%s", "%t", "%y", "%z", "%H", or "%M".

                  The "%" character can be omitted if the format pattern is combined with literal characters or other format patterns.

                  \ c
                  Where c is any character. Displays the character literally. To display the backslash character, use "\\".

                  [/html]

                  Thnx so much ...

                  Hey i already have a date, if i want to update that date into other db then, wat shd be the changes..
                  Private Function updateDate(ByVa l strDate As String) As String
                  'to convert valid date format
                  Dim xdate As String
                  Try
                  .....>>> xdate = DateTime.Now.To String("yyyy-MM-dd") .....>> (DOUBT)
                  Catch ex As Exception
                  xdate = "'" + "0000-00-00" + "'"
                  End Try
                  updateDate= xdate
                  Return updateDate
                  End Function

                  Comment

                  Working...