+ Reply to Thread
Results 1 to 26 of 26

Convert date and time

  1. #1
    Registered User
    Join Date
    10-01-2020
    Location
    cameroun
    MS-Off Ver
    office 2007
    Posts
    37

    Convert date and time

    Hello everyone.
    I have the following problem.
    I have the date in the format (dd.mm.yyyy) and the time in the format (hh: mm, AM) each in its cell.
    I would like to exploit them in a vba macro on this format: (mm / dd / yyyy hh: mm AM) in a new variable ScheduleTime = "(mm / dd / yyyy hh: mm AM)" without modifying the format of the original cells


    Please Login or Register  to view this content.

  2. #2
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,811

    Re: Convert date and time

    What exactly is in Q4 and I29? Are they actual date/time serial numbers or are they text strings? Many of these questions usually end up being more about converting text to a date/time serial number that Excel (and VBA) can use, so many of these questions often start by needing to know exactly what is in the cell(s) in question.

    You say that you want to "exploit the dates in a specific format". Exactly what do you mean by this? When stored as date/time serial numbers or as date type variables in VBA, the value does not really have a specific format. Normally one stores the date/time data as serial numbers, performs the needed calculations, then stores the result as a serial number and formats in the desired format. It might be important to understand exactly what you are trying to do and why format is important (whether the format is only important at the final output stage or if something about the format is essential to the processing steps).
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  3. #3
    Registered User
    Join Date
    10-01-2020
    Location
    cameroun
    MS-Off Ver
    office 2007
    Posts
    37

    Re: Convert date and time

    Voila comment j'ai éssayé
    mais ca ne marche pas
    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    10-01-2020
    Location
    cameroun
    MS-Off Ver
    office 2007
    Posts
    37

    Re: Convert date and time

    in Q4 the date is stored in date (dd mmm yyyy) while in I29 the time is stored in hour (hh: mm)
    I want to use my data on another platform, in particular an API for sms. the date format for programming the sending of sms is as follows (mmm dd yyyy h: mm).
    I would like to do it directly in vba so as not to upset my data

  5. #5
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,811

    Re: Convert date and time

    I don't see a text() function in VBA, so I expect that statement would fail. What do you get from Scheduletime=worksheets("PLANNING").Range("Q4").value2? What do you get from =ISTEXT(Q4) entered in any convenient cell?

    Assuming that Q4 and I29 contain date/time serial numbers, I would expect the procedure to look something like
    Please Login or Register  to view this content.
    Most problems with code like this will be when Q4 and I29 contain text strings and not numbers.

  6. #6
    Registered User
    Join Date
    10-01-2020
    Location
    cameroun
    MS-Off Ver
    office 2007
    Posts
    37

    Re: Convert date and time

    Thank you very much
    I replaced value2 with string because it reported a type mismatch.
    Please now, I would to get it in this format : mmm/dd/yyyy h:mm, with the space between date and time

  7. #7
    Registered User
    Join Date
    10-01-2020
    Location
    cameroun
    MS-Off Ver
    office 2007
    Posts
    37

    Re: Convert date and time

    This is how I coded but my api does not retrieve the output date. here is the expected format: mm / dd / yyyy h: mm

    Please Login or Register  to view this content.

  8. #8
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,408

    Re: Convert date and time

    Quote Originally Posted by rodryg123 View Post
    in Q4 the date is stored in date (dd mmm yyyy)...
    Is the value in Q4 a real Excel date that you Custom Formatted to display with dots instead of slashes between the day, month and year? Or did you just type or have your code place in Q4 a number followed by a dot followed by a number followed by a dot followed by a number?



    Quote Originally Posted by rodryg123 View Post
    ...while in I29 the time is stored in hour (hh: mm)
    Pretty much the same question here as well. Is the value in I29 a real Excel time value that you Custom Formatted to display with a space after the colon and a comma after the month number or did you type or have your code place in I29 a number followed by a colon followed by a space followed by a number followed by a comma followed by a space followed by either the letters AM or PM?

  9. #9
    Registered User
    Join Date
    10-01-2020
    Location
    cameroun
    MS-Off Ver
    office 2007
    Posts
    37

    Re: Convert date and time

    in Q4 the value is a real date generated by the time and the system of my bone.
    While in i29 I entered 7:00 and customized to have the format 7:00 AM

  10. #10
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,811

    Re: Convert date and time

    If it's a real date and a real time in Excel, then I would not convert to a string while importing to VBA. Excel and VBA tend to work with dates and times best when they are stored as unformatted numeric data types (double in this case) or date data types. Once converted to a string, dates become more difficult to work with.
    Please Login or Register  to view this content.
    I've changed the data types so that VBA should store the dates and times as doubles. The final result is converted to text at the end. I've also added a Stop statement so you can step through the code and see what each line is doing and take advantage of VBA's debugging tools.

  11. #11
    Registered User
    Join Date
    10-01-2020
    Location
    cameroun
    MS-Off Ver
    office 2007
    Posts
    37

    Re: Convert date and time

    Thank you. it works.
    But the date is retrieved in this format (mm.dd.yyyy) however I would like to retrieve the scheduleTime date in this format (mm/dd/yyyy) i.e. replace the (.) By the (/)

  12. #12
    Registered User
    Join Date
    10-01-2020
    Location
    cameroun
    MS-Off Ver
    office 2007
    Posts
    37

    Re: Convert date and time

    On the other hand, the if condition above, I would like that when it is not checked the program does not deliver a message to the clients


    Please Login or Register  to view this content.

  13. #13
    Registered User
    Join Date
    10-01-2020
    Location
    cameroun
    MS-Off Ver
    office 2007
    Posts
    37

    Re: Convert date and time

    i have the same problem here. when the condition is not ok, the program send a sms empty

    Please Login or Register  to view this content.

  14. #14
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,811

    Re: Convert date and time

    But the date is retrieved in this format (mm.dd.yyyy) however I would like to retrieve the scheduleTime date in this format (mm/dd/yyyy) i.e. replace the (.) By the (/)
    I don't understand this. Using a double data type, the date read in (the value in datein) should have no format -- just an integer. Likewise, the value of timein should have no format -- just a number between 0 and 1. VBA has no idea that these represent dates until you get to the "scheduletime=Format(...)" statement where the number is converted to text in the specified format. If you want "/" delimiting the parts of the date, it should be a simple matter of entering the appropriate number format code "mm/dd/yyyy h:mm".

    As for the message sending part, I'm not sure I understand that part either. What is in P32 (post #12)? From what I can see, in post #12, the code assigns a value to Message when P32 contains something that can be interpreted as "False", and does nothing when P32 is "True". In post #13, the code will assign the specified text to Message when the time of execution is after 18:00. It isn't clear to me what you want to be different here.

  15. #15
    Registered User
    Join Date
    10-01-2020
    Location
    cameroun
    MS-Off Ver
    office 2007
    Posts
    37

    Re: Convert date and time

    actually datein is retrieved in number = 44120, and timein = 0.291666666667 however at scheduletime = 10.16.2020 7:00
    I don't know if it's related to the date format of my OS.

    P32 is today's date.
    My wish is that if the date of the appointment is different from p32, the program sends a message to the API whose delivery is scheduled at 7:00 on the exact day of the appointment. Otherwise it does nothing
    Please Login or Register  to view this content.

    In the other case, if the program is closed after 6 p.m., it manages the report for the day. If we close before nothing happens.
    Except that when I close before 6 p.m. it generates an SMS without the information. No, I wouldn't want him to deliver the sms at all in this case

    Please Login or Register  to view this content.

  16. #16
    Registered User
    Join Date
    10-01-2020
    Location
    cameroun
    MS-Off Ver
    office 2007
    Posts
    37

    Re: Convert date and time

    Here is the date format required by the API to schedule the sending of sms.

    // Step 5: Specify the time to send the sms. Format $ format = 'm / d / Y h: i A';
    $ schedule_time = '07 / 21/2020 11:12 AM ';

    / *
    here is a piece of code to help you convert the date to the required format

    $ originalDate = "2009-02-15 15:16:17";
    $ newDate = date ("m / d / Y h: i A", strtotime ($ originalDate)); /
    $ schedule_time = $ newDate;
    * /

  17. #17
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,811

    Re: Convert date and time

    actually datein is retrieved in number = 44120, and timein = 0.291666666667 however at scheduletime = 10.16.2020 7:00 I don't know if it's related to the date format of my OS.
    What are you using in your Format() function when you assign dateout to scheduletime? It's possible that your OS's regional settings are impacting this, but I don't see the same problem on my computer. I can assign a date format that is different from my OS settings without difficulty. I'm not sure why your copy of Excel/VBA would do something different. Have you tried changing your OS settings?

    As for the decision tree, for the post #12 case, it is testing if "rowdaterdv" (read in from Q4) is the same as "today" in P32. If it is always executing the "Else" branch, that means it is seeing that rowdaterdv and the value in P32 are not equal. Debugging this will require knowing exactly what is in rowdaterdv and exactly what is in P32 to determine if and why VBA is making an "incorrect" choice here.

    For the post #13 case, it again will require debugging exactly what now-date is returning and exploring why that is different from expected.

    Perhaps another observation. I have no experience sending text messages using VBA, but I don't see any code in what you have shared that seems to send the message. What you have shared assigns a value to the variable "Message" (or does not assign anything to message depending on the If condition), but I don't see anything that seems to actually send the message. Is it possible the, for this question, you need to be looking elsewhere for the code that actually sends the message and include the If condition there?

  18. #18
    Registered User
    Join Date
    10-01-2020
    Location
    cameroun
    MS-Off Ver
    office 2007
    Posts
    37

    Re: Convert date and time

    I looked at my system does not have the date format with slash maybe that's the cause. I will test on another pc later.

    rowdaterdv contains the date of the appointment


    Here is the code for sending sms, it just works for the sending planning concerns and the coding tree

    Please Login or Register  to view this content.

  19. #19
    Registered User
    Join Date
    10-01-2020
    Location
    cameroun
    MS-Off Ver
    office 2007
    Posts
    37

    Re: Convert date and time

    How to make that when a condition is not check the execution point directly on end sub

  20. #20
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,811

    Re: Convert date and time

    How to make that when a condition is not check the execution point directly on end sub
    That sounds like you want an Exit Sub statement https://docs.microsoft.com/en-us/off...exit-statement Something like
    Please Login or Register  to view this content.

  21. #21
    Registered User
    Join Date
    10-01-2020
    Location
    cameroun
    MS-Off Ver
    office 2007
    Posts
    37

    Re: Convert date and time

    Hi,
    So I tried a small macro to force the format change, but so far it doesn't work

    Please Login or Register  to view this content.

  22. #22
    Registered User
    Join Date
    10-01-2020
    Location
    cameroun
    MS-Off Ver
    office 2007
    Posts
    37

    Re: Convert date and time

    please I have also the following situation which I ask for your help:
    I have a list which contains 3000 records (row).
    Column A contains the last and last names of the customers.
    Column B contains the telephone numbers
    Column C contains the customer's birthday.
    I want the following conditionality tree:
    The program at start-up browses the list, compares the current date with those entered in column C; in the event of a tie for example for Cx, this would mean that it is the customer's birthday. Then the program retrieves the information Ax (client name) and Cx (client name) so that it can be used in the automatic SMS sending system

  23. #23
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,811

    Re: Convert date and time

    Quote Originally Posted by rodryg123 View Post
    Hi,
    So I tried a small macro to force the format change, but so far it doesn't work

    Please Login or Register  to view this content.
    I'm not sure exactly what you mean by "doesn't work". Usually if changing the number format does not change what you see in the cell, that means the date stored in the cell is a text string and not a real date/time serial number. This is usually fixed by correctly entering the data into the cell so it is a real date time serial number. Strategies for converting text to number/date (being aware of how you system's regional settings impact some of these strategies): https://support.microsoft.com/en-us/...rs=en-us&ad=us

    As for the other issue, again, assuming the dates in column C are real dates/times, the test for "is today the same as the date in column C" should be something like [worksheet formula] TODAY()=C2 [VBA statement] If Date = worksheet.cells(2,3).value then do something
    Last edited by MrShorty; 10-18-2020 at 12:40 PM.

  24. #24
    Registered User
    Join Date
    10-01-2020
    Location
    cameroun
    MS-Off Ver
    office 2007
    Posts
    37

    Re: Convert date and time

    thank for your reply. But i think this instruction will just stop at a cell, i would like the program to go through the whole list of clients

  25. #25
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,811

    Re: Convert date and time

    Nest something similar to that statement inside of a loop:
    Please Login or Register  to view this content.

  26. #26
    Registered User
    Join Date
    10-01-2020
    Location
    cameroun
    MS-Off Ver
    office 2007
    Posts
    37

    Re: Convert date and time

    voila la boucle que j'ai éssayé, mais il bloque au niveau de next i avec pour message d'erreur : next i sans for.

    cette condition doit me permettra de tester uniquement le jour et le mois pour savoir si c'est l'anniversaire car ma range E est formatée a (jj.mmm.1900)
    Please Login or Register  to view this content.
    as mentioned above, the goal is to test line by line by line. If the condition is verified, send the message and continue the procedure until the last line.
    If the condition is not verified, skip the line and continue the procedure







    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 2
    Last Post: 02-06-2020, 10:04 AM
  2. Replies: 7
    Last Post: 06-19-2017, 06:10 PM
  3. [SOLVED] Convert Unix Date-time stamp to excel date and/or time columns
    By judikz in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 05-13-2015, 05:46 PM
  4. Convert Julian date/ time to regular date/ time (MM/DD/YYYY HH:MM:SS)
    By dataguy30 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 03-07-2013, 11:33 AM
  5. [SOLVED] How do I convert imported date/time data to date/time format?
    By andykent99 in forum Excel General
    Replies: 4
    Last Post: 01-03-2013, 07:47 AM
  6. Convert Julian Date/Time (ddd.tttttt) to Calendar Date/Time
    By LindseyW in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-12-2009, 09:30 PM
  7. Replies: 0
    Last Post: 08-23-2005, 12:22 PM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1