I need to get todays date + 14 days string formatted in standard dd.mm.yyyy format into Libre Office calc cell.
I already tried the code below, but I lack the knowledge to cope with "Object variable not set" error.
REM ***** BASIC *****
sub Datumplus14
rem ----------------------------------------------------------------------
rem define variables
dim document as object
dim dispatcher as object
Dim cell as object
dim term as date
rem ----------------------------------------------------------------------
rem get access to the document
document = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
rem ----------------------------------------------------------------------
term = today()
cell.String = DateAdd("d", 14, datum)
dispatcher.executeDispatch(document, ".uno:EnterString", "", 0, cell)
end sub
Different ideas on how to get this done instead of macro is welcome as well.
Hmm, not sure if this will answer the specifics of what you're actually trying to do, but you can easily get the current date and just +14 to it directly in a cell formula.
Like so:
=NOW()+14
The rest is just applying the required date format to that cell. You can also grab the date from another cell too.
Function myFunction() As String
myFunction = Format(Now()+14, "dd.mm.yyyy")
End Function
Related
I need to create a report(.txt) and I want to reference each sessions of tests, so I wanted that for each simulations, I add the date to name of my report.
Like Report_01-19-2017-12:53.txt
So far I have been able to create either a file with the date inside with :
$system("date > sDate");
or display it on my simulation software with :
$system("date");
So far,My code look like :
string filename;
reg [8*30:1] data; // the date is of 29 characters in size
string sDate;
integer scan_file,data_file ;
initial begin
$system("date > data");
data_file = $fopen("data", "r");
scan_file = $fscanf(sDate,"%s", data);
$fclose("data");
[...]
filename = {filename,sDate};
Report_Task = $fopen(filename,"a"); [...]
end
sDate contains nothing, date contains the date...
I tried string and reg for filename and sDate
Instead of going through files to get the date, you could use svlib. Chapter 9 of the documentation illustrates how to get information about the current time.
Don't you mean
scan_file = $fscanf(data_file, "%s", sDate);
where, if the read is successful, scan_file will be equal to 1 (the number of items read). (So, you probably didn't want to call it scan_file.)
I have a database that I need to store a bunch of times and do calculations based on the times, however, the times can be over different days so using just a time format wont calculate the correct difference between the two. For ease of use I wanted to limit how much of the date they have to input because there is a bunch of times and I dont want to have to type the full date each time. I created the following custom format: 16-Jan 15:00 (d-mmm h:nn)
I also created a custom input mask to go with this: 90-LLL\ 90:00
My form fields have the same format and input masks to match. The initial input of the field works right, the issue comes when I try to edit a field and change one digit. It pops up with the error that it doesnt match the input mask, even though it does. In order to change the field I have to delete everything, remove focus from the textbox and then click back in. Is there anyway to fix that? Or is there another option to calculate the difference between the times without having to use the date when it could be over 24hrs long (not longer than 48 though)
Yes, you can have a checkbox, NextDay, to mark if the end time is of the following day.
Then your timespan will be:
Dim Timespan As Date
Timespan = CDate([EndTime] - [StartTime] + Abs([NextDay]))
To input the time, you can use the method here: Entering 24-hour time
To format and display your timespan, also for values above 24 hours, use a function like this:
Public Function FormatHourMinute( _
ByVal datTime As Date, _
Optional ByVal strSeparator As String = ":") _
As String
' Returns count of days, hours and minutes of datTime
' converted to hours and minutes as a formatted string
' with an optional choice of time separator.
'
' Example:
' datTime: #10:03# + #20:01#
' returns: 30:04
'
' 2005-02-05. Cactus Data ApS, CPH.
Dim strHour As String
Dim strMinute As String
Dim strHourMinute As String
strHour = CStr(Fix(datTime) * 24 + Hour(datTime))
' Add leading zero to minute count when needed.
strMinute = Right("0" & CStr(Minute(datTime)), 2)
strHourMinute = strHour & strSeparator & strMinute
FormatHourMinute = strHourMinute
End Function
I have a cell in an Excel file that I want to populate with the current date&time and the user name. The cell is populated by clicking a button.
Until now I was not using the function Application.UserName and the date & time output was formatted correctly (e.g: 07.01.2014 16:57)
After I have added the & Space(1) & Application.UserName piece of coding the formatting of date & time is not formatted the way I want (depending on the user that populates the cell I get results like: 09-Jan-14 11:30:13 + USERNAME or 1/7/2014 5:59:43 PM + USERNAME)
I guess that it takes the formatting from the user's regional settings, correct ?
My question is how can I override those settings as it looks that the ActiveCell.NumberFormat = "dd.mm.yyyy hh:mm" piece of code is no longer working good.
Below is my code:
Private Sub CommandButton1_Click()
ActiveCell = Now() & Space(1) & Application.UserName
ActiveCell.NumberFormat = "dd.mm.yyyy hh:mm"
End Sub
The Date/Time NumberFormat will only work if the cell has a valid Date/Time. So the trick is to change the format first and then add the username.
Is this what you are trying?
With ActiveCell
.Value = Now()
.NumberFormat = "dd.mm.yyyy hh:mm"
.Value = .Text & Space(1) & Application.UserName
End With
I'm trying to get the current month as a short string in a Visual Basic Script:
Dim month
Dim mth
month = Now.Month ' This doesn't work.
month = Now().Month ' Tried this too.
month = Month(Now) ' Also tried this.
mth = MonthName(month, True) ' (e.g. "Apr" or "Mar")
However, I keep getting runtime errors:
Microsoft VBScript runtime error: Object required: 'Now'
Microsoft VBScript runtime error: Object required: 'Now()'
Microsoft VBScript runtime error: Type mismatch 'Month'
I'm able to use Now fine as a string:
CStr(Now)
Or as a plain value
Dim val
val = Now
How can I use Now as an Object for accessing its member functions? Perhaps I am confusing Visual Basic functionality with VB Script?
VBScript Dates are not objects - use the Month(SomeDate) function to get the month number of SomeDate:
>> WScript.Echo Month(Now)
>>
4
To get the month's name (abreviated or full), use:
>> WScript.Echo MonthName(Month(Now()),False)
>> WScript.Echo MonthName(Month(Now()),True)
>>
April
Apr
(stolen from #collapsar, but using the correct (boolean) type of the second parameter).
Official Docs for
Month(): here
MonthName(): here
[The samples given are 'living' code, if you get errors, your code is to blame]
try this line of code:
MonthName(Month(Now()),1)
a standalone solution:
Dim imonth
Dim mth
imonth = Month(Now()) ' Also tried this.
mth = MonthName(Month(Now()))
WScript.Echo "mth = " & mth
WScript.Echo "mth_short = )" & MonthName(Month(Now()),1)
The issue is that Visual Basic Script is not case sensitive and my variable month was colliding with the built in Month.
As given here and here
Syntax:
Month(Now)
I need to print the date in the format of mm/dd/yyyy.
if the date is 4/24/2009 it should print the date as 04/24/2009.
that is zero padding is also needed..
I used date function to get the current date...but the date is getting in the format of m/dd/yyyy...
Tested in the immediate window and is working for me (output as a comment)
Format(Now, "MM/dd/yyyy") '04/29/2009
Format(Date, "MM/dd/yyyy") '04/29/2009
Format(CStr(Now), "MM/dd/yyyy") '04/29/2009
Format(Date$, "MM/dd/yyyy") '04/29/2009
Format(CDate(Date), "MM/dd/yyyy")'04/29/2009
So whether it is string or datetime should not matter.
Edit: Saw your comment to Fredrik. It doesn't matter how it looks like when you save it to the db table (column date format would be a property of the db and not your program's (or vb's) responsibility). Just format the value as and when you retrieve it from the db.
Note that the "/" character in date formatting functions has a special meaning, as "date separator". This means that i may be replaced with the date separator for the current locale that the program is executed in (here in Sweden it would be replaced with "-" for instance). In order to ensure that you do indeed get the "/" character in the output, I think this would work (I don't have a VB installation to verify with):
Format(date, "MM'/'dd'/'yyyy")
just for the record, escaping the slash will work
Format(dt,"MM\/dd\/yyyy")
Try the next code:
Format(dt,"MM/dd/yyyy")
When you enter date in whatever format it will convert default value so do one thing that in access change your data type date/time to text then it can't affect to your work sure.
I also use VB6 and need to format date in my txt report
this works for me
Format$(Now, "yyyy-mm-dd-00.00.00")
but only if I declare date as string
You can try like this also depending upon your requirement
Dim strDate As String
Dim strDate1() As String
strDate = FormatDateTime(Now, vbGeneralDate)
If InStr(strDate, " ") > 0 Then
strDate1 = Split(strDate, " ")
Dim datDate1 As Date
If Month(strDate1(0)) < 10 Then
txtDate.Text = "0" + strDate1(0)
Else
txtDate.Text = strDate1(0)
End If
Else
End If
Formatting DateTime as a string is straightforward. Often we use format patterns like "HH." But methods like ToShortDateString are also useful.
Example. First we get the current time through DateTime.Now. When you execute these code examples, the current DateTime will be different on your computer.
Here: A format string beginning with MMM (for the month) is used. Look at how the pattern matches up to the output of the program.
VB.NET program that uses format string with DateTime
Module Module1
Sub Main()
' Use current time.
' ... Use a format.
' ... Write to console.
Dim time As DateTime = DateTime.Now
Dim format As String = "MMM ddd d HH:mm yyyy"
Console.WriteLine(time.ToString(format))
End Sub
End Module
Output
Feb Tue 21 13:26 2017
strDate = Format(strDate, "yyyy-mm-dd")
BillTime = Format(BillTime, "hh:mm:ss")