I have a VB program built in Studio 2017. I need to generate the time in a format which can go on to be used in a filename.
DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
spits out 12:00:00 for example and the : isn't usable in a filename.
I could do with either removing the : so it would be 1200000 (not particularly readable but suitable for my purposes) or 12-00-00.
I have checked here and can't see any ToString format that will do the trick.
My code will put a label (say label1) to the current date and time. Another part will use the Label1.Text to grab the string. So any formatting can happen with Label1.
For example, it will be used as follows;
oDoc = oWord.ActiveDocument
oDoc.saveas2("C:\Test\" & "DocumentTitle" & "-" & label1.text & ".docx"
Is there a way to format the Date string to what I want?
Why add the colons in the first place?
DateTime.Now.ToString("yyyy-MM-dd HHmmss")
Simply use the String.Replace
Dim ActualTime as String = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")
ActualTime = ActualTime.Replace("/","-").Replace(":","-")
oDoc = oWord.ActiveDocument oDoc.saveas2("C:\Test\" & "DocumentTitle" & "-" & ActualTime & ".docx"
Not the most optimized but clear to understand. Added a one line replace as suggested by Visual Vincent.
Make sure to check his solution as well.
Related
I have an API that I want to update dynamically so that the user can enter a start date and an end date on a spreadsheet and my macro will pull back data for that particular date range.
The issue I'm having is that within the API URL the StartDate and EndDate parameters must be in the format yyyy-mm-dd as a string.
I've tried URL = "https:// ...&StartDate = Format(Date(),"yyyy-mm-dd") & EndDate=Format(Date(),"yyyy-mm-dd")&..." (the ... is for the things before and after the URL).
An example of the type of URL I'm looking at is:
https://www.googleapis.com/analytics/v3/data/ga?ids=ga:12345&startdate=2008-10-01&end-date=2008-10-31&metrics=ga:sessions,ga:bounces
I've also played around with adding in extra quotes within the URL string but I can't seem to get it to work.
I keep getting told that the dates aren't being recognised and therefore I can only get the code to run if I hardcode dates.
Any suggestions?
I noticed a few issues in the code posted. The & is the concatenation operator in VBA. You need to enclose that in "" to make sure you are returning the ampersand as a string, and not joining strings together.
I've added some sample code which hopefully illustrates the idea and get's you back up and running. The code should print out True if the createdURL and testURL are equal, or False if not.
Code
Option Explicit
Public Sub FormatExample()
'This is the example provided
Dim testURL As String
testURL = "https://www.googleapis.com/analytics/v3/data/ga?ids=ga:12345&" & _
"startdate=2008-10-01&end-date=2008-10-31&metrics=ga:sessions,ga:bounces"
'This is a built string example
Dim createdURL As String
createdURL = "https://www.googleapis.com/analytics/v3/data/ga?ids=ga:12345" & _
"&startdate=" & Format(#10/1/2008#, "yyyy-mm-dd") & _
"&end-date=" & Format(#10/31/2008#, "yyyy-mm-dd") & _
"&metrics=ga:sessions,ga:bounces"
'Print out if they are equal
Debug.Print createdURL = testURL
End Sub
Good morning,
When I want to show the project name & version I use:
System.Windows.Forms.Application.ProductName
System.Windows.Forms.Application.ProductVersion
Is there a similar thing for the last changed date of a project?
At the moment I have a valid workaround (see below), but I wonder if there is a build in solution like with the ones above.
Dim strFile = Application.StartupPath & "\" & Application.ProductName & ".exe"
Return System.IO.File.GetLastWriteTime(strFile.ToString()).ToShortDateString()
I am adjusting an existing file transfer windows service that renames the file being sent as a timestamp. For testing purposes, I need to make the files sent show up in the destination directory as being ten days ahead of when they were actually sent. Ex: if it's sent on 11/23/2015, it needs to look like it arrived 12/03/2015.
The line of code that generates the file name looks like this:
Dim strFileNameToTransfer As String = My.Settings.FileDirectory.ToString() & Format(Now(), "yyyy") & Format(Now(), "MM") & Format(Now(), "dd")
File name shows up in directory like this, if sent on 11/23/2015:
"20151123.xml"
But I would need it to show up like this:
"20151203.xml"
It would need to adjust the month as well, since the test will cross over into December and it is now November.
Like I said, this is for testing purposes, so it needs to go back to the way it was when testing is over. I really just need a quick fix here, but I know zero about Visual Basic, and I'm still new to programming in general as well. Help!
All you have to do is add 10 days to the existing date and use that date for your file name. You can also don't need to split the formatting into three different strings.
Dim fileDate = Now().AddDays(10)
Dim strFileNameToTransfer As String = My.Settings.FileDirectory.ToString() & Format(fileDate, "yyyyMMdd")
EDIT:
gmiley is right, it is better to use Path.Combine instead of string concatenation
Dim NameToTransfer As String = System.IO.Path.Combine(My.Settings.FileDirectory.ToString(), String.Format("{0}.{1}", fileDate.ToString("yyyyMMdd"), "xml"))
I need some ideas on how I can accomplish what I'm attempting to do, here is the scenario.
I have 2 DateTimePickers (DateTimePicker1 and DateTimePicker2) to act as a date range. I need a function to :
1.) Download a file from my ftp with the initial date/time in yyyyMMdd.txt format
2.) Add 1 to the date and do the download again until DateTimePicker1 = DateTimePicker2
3.) If the file doesn't exist (its possible it may not), just move on to the next date.
I managed to create an infinite loop! It just won't repeat. I'm not familiar with Do While loops so any tips are weclomed!
I tried something like:
While DateTimePicker1 < DateTimePicker2
Do My.Computer.Network.FileDownload("ftp://address/", "ftp://address/" & folder & "/" & DateTimePicker1 & ".txt")
DateTimePicker1.Value.AddDays(1)
Loop
I think you need to compare the Value properties of the DateTimePickers and when you call AddDays() you have to assign the result back to the Value property like this:
DateTimePicker1.Value = DateTimePicker1.Value.AddDays(1)
The Value property is a DateTime object and AddDays() returns the DateTime object as a result.
So try this...
While DateTimePicker1.Value < DateTimePicker2.Value
My.Computer.Network.FileDownload("ftp://address/", "ftp://address/" & folder & "/" & DateTimePicker1 & ".txt")
DateTimePicker1.Value = DateTimePicker1.Value.AddDays(1)
End While
Here's a Fiddle Demo with just DateTime objects.
I coded something using Date statement in Access VBA. It was working fine until the start of this month, but now I am seeing that the Date has automatically changed the format from dd/mm/yyyy to mm/dd/yyyy. Has anyone else encountered the same problem?
The default Access SQL date format, regardless of locale, is mm/dd/yyyy. If you use an invalid date format, it will 'helpfully' try to convert that to a valid date for you.
So, if you use '30/09/2008', it will recognize you're using dd/mm/yyyy, and convert it appropriately. However, a value like '10/01/2008' is a valid mm/dd/yyyy value to begin with, so it will not be converted, and stored incorrectly in case you actually meant dd/mm/yyyy....
The solution is to always convert your date values to a mm/dd/yyyy string prior to using them in Access SQL statements. You have to be a bit careful here, as using VBA date format masks may not work entirely as you'd expect on non-US locales (e.g. 'helpfully' interpreting "mm/dd/yyyy" as "the localized short date format"), so please test carefully using your particular Access/VBA version.
Access requires a date to be unambiguous. It is generally recommended that you use yyyy/mm/dd, regardless of locale. For example:
strSQL="SELECT SomeDate FROM tblT WHERE SomeDate=#" & Format(DateVar, "yyyy/mm/dd") & "#"
Try this code:
stLinkCriteria = "[ProjectDate] Between #" & Format(CDate(Me![txtDateFrom]), "mm/dd/yyyy") & "# And #" & Format(CDate(Me![txtDateTo]), "mm/dd/yyyy") & "#"
It works for me.
This works:
sentenciaSQL = "UPDATE Numeraciones " & _
"SET Valor = " & Valor & ", " & _
"Fecha = #" & **Format(fecha,"mm/dd/yyyy HH:nn:ss") & "#, " &** _
"Id_Usuario = " & Id_Usuario & _
" WHERE Nombre = '" & Nombre & "'"
I have been very successful using the datevalue() function. When getting dates from unbound controls it seems to be clever enough to interpret the format "dd/mm/yyyy" correctly. Thus, a Jet SQL query like
"Select * from DateTable where StartDate = datevalue(" & me!TxtStartDate & ");"
seems to work every time.
I was experiencing same issue while trying to build a SQL string through VBA.
My locale settings use dd/mm/yyyy and I was trying to put into SQL statement data taken from an unbound textbox in a form.
The issue was due to the fact I was declaring, let's say, varMyDate as "date" type, so the engine reverted the format back even after the format.
Since you are really building a string the logical and proper data type is "string", and that solved the problem.