dcsimg
Results 1 to 10 of 10
  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2012
    Location
    London, UK
    Posts
    135

    Cannot add minutes to date and convert to UK format.

    Hi,

    I've spent ages trying piecemeal code but can only get this to work if I use current date and time.

    I am reading a value from a config file where I do not control the date/time format.

    Current format from config is this:

    19-January-2018 04:00:03 PM

    So to that date value, I'd like to add 500 minutes using a UK format so my result would look like:

    20/01/2018 00:19:01

    I'm sure this is trivial for most but really tired and can't seem to get this to work.

    Many thanks!
    J

  2. #2
    Frenzied Member
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    1,076

    Re: Cannot add minutes to date and convert to UK format.

    What variable types are you storing this data in? Post the code you are using.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2012
    Location
    London, UK
    Posts
    135

    Re: Cannot add minutes to date and convert to UK format.

    hiya,

    Ah, good question!!

    Well, for the working one I'm simply:
    Code:
    dim dDate as datetime = DateTime.Now()
    dDate = dDate.AddMinutes(500)
    dDate .ToString('yyyyMMddhhmmss')  'change this later to UK
    But when I read from config, my value is a string so let's say:
    Code:
    dim sMyDate as string = "19-January-2018 04:00:03 PM"
    Cheers,
    J

  4. #4
    Frenzied Member
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    1,076

    Re: Cannot add minutes to date and convert to UK format.

    You would need to convert the string data you are reading in to a datetime variable. Then you can add the 500 minutes and convert the result back to a string in whatever format you need to write back out.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jan 2012
    Location
    London, UK
    Posts
    135

    Re: Cannot add minutes to date and convert to UK format.

    Indeed. But I don't get how I'm meant to handle the - and AM/PM from just a string? Now it crashes .net to the point where I have to close it and open it again.

    Do I need to chop this string up into separate variables? Seems a bit messy? This crashes:
    Code:
    Dim sMyDate As String = "19-January-2018 04:00:03 PM"
    Dim dNewDate As DateTime = DateTime.ParseExact(sMyDate, "dd-MM-yyyy hh:mm:ss tt", CultureInfo.InvariantCulture)

  6. #6
    Frenzied Member
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    1,076

    Re: Cannot add minutes to date and convert to UK format.

    Possibly useful links:

    Convert String to datetime:

    https://msdn.microsoft.com/en-us/lib...v=vs.110).aspx

    Convert datetime to String:

    https://msdn.microsoft.com/en-us/lib...v=vs.110).aspx

  7. #7
    Frenzied Member
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    1,076

  8. #8
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    98,667

    Re: Cannot add minutes to date and convert to UK format.

    Dates don't have formats. They are binary values; just numbers. If you want to see a date in a particular format then you need a String representation of a Date. Basically, what you need to do is take the String you have to start with, convert that to a Date, perform the appropriate arithmetic with that, then convert the result to a String. If you know for a fact that your initial String is a valid representation of a date then call Date.ParseExact to convert it to a Date. If you're not sure, call TryParseExact. You can then use the code that you have in the first snippet in post #2, except you have to assign the result of ToString somewhere.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    31,607

    Re: Cannot add minutes to date and convert to UK format.

    Quote Originally Posted by jonesin View Post
    Indeed. But I don't get how I'm meant to handle the - and AM/PM from just a string? Now it crashes .net to the point where I have to close it and open it again.

    Do I need to chop this string up into separate variables? Seems a bit messy? This crashes:
    Code:
    Dim sMyDate As String = "19-January-2018 04:00:03 PM"
    Dim dNewDate As DateTime = DateTime.ParseExact(sMyDate, "dd-MM-yyyy hh:mm:ss tt", CultureInfo.InvariantCulture)
    That's because the format you passed ParseExact doesn't match the date....

    Code:
            Dim sMyDate As String = "19-January-2018 04:00:03 PM"
            Dim dNewDate As DateTime = DateTime.ParseExact(sMyDate, "dd-MMMM-yyyy hh:mm:ss tt", CultureInfo.InvariantCulture)
    You have a complete month name... NOT a two digit value, so it needs to be MMMM.


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  10. #10
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    98,667

    Re: Cannot add minutes to date and convert to UK format.

    Hmmm... it appears that I managed to overlook post #5.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width


×
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.