lua program shows current time - date

This is a piece of lua script that displays the time. I cannot separate the numbers ie: time.hour, ":",
to basically show hh:mm:ss
time = os.date("*t")
print(time.hour .. time.min .. time.sec)

There are several ways to do this:
Use string concatenation: print(time.hour .. ":" .. time.min .. ":" .. time.sec)
Use formatting: print(("%02d:%02d:%02d"):format(time.hour, time.min, time.sec))
Use table concatenation: print(table.concat({time.hour, time.min, time.sec}, ":"))
When you really need to format your string, my preference would be for #2. For time = {hour = 1, min = 20, sec = 5} this prints:
1:20:5
01:20:05
1:20:5

For simply printing the time - extract what you want (the time) from the full date stamp string:
> os.date():sub(9)
12:30:39
This works on my PC ;). There may be a different date stamp string in your OS.
G

local date = os.date('*t')
local time = os.date("*t")
print(os.date("%A, %m %B %Y | "), ("%02d:%02d:%02d"):format(time.hour, time.min, time.sec))`

Related

Suppressing unwanted spaces in dates

From reading code from elsewhere, I have a matrix of dates called 'time' that have unwanted spaces that I want removed.
I've tried isspace and regexprep with no luck
time = regexprep(time, '\W', '');
I have about 130000 dates with the following format:
04-July -2017 09:54:30.000
04-July -2017 09:54:31.000
etc
There are two spaces between the end of 'July' to the next dash I want to suppress to:
04-July-2017 09:54:30.000
04-July-2017 09:54:31.000
Replace two or more spaces with nothing:
>> time = {'04-July -2017 09:54:30.000'
'04-July -2017 09:54:31.000'}
>> regexprep(time,' {2,}','')
{'04-July-2017 09:54:30.000'}
{'04-July-2017 09:54:31.000'}
Unless you just want to correct your input file for later usage, you do not necessarily need to correct the input. There are several ways to parse the time directly with the extra spaces:
Let time be:
time = ['04-July -2017 09:54:31.000';
'04-July -2017 09:54:32.000']
Then to directly parse the string representation of the datetime into a MATLAB date serial number you can use:
%% get date in [MATLAB date serial number]
formatIn = 'dd-mmm -yyyy HH:MM:SS.FFF' ;
matlabTime = datenum(time,formatIn)
matlabTime =
736880.412858796
736880.41287037
This serial time representation is not so human readable but it's the fastest thing you can get if you want to do calculations with date/time.
If your goal is simply to correct the string, then you can use the same trick to read the value in, and define exactly which output format you want out:
%% get date in [string]
formatIn = 'dd-mmm -yyyy HH:MM:SS.FFF' ;
formatOut = 'dd-mmm-yyyy HH:MM:SS.FFF' ;
stringTime = datestr(datenum(time,formatIn),formatOut)
stringTime =
04-Jul-2017 09:54:31.000
04-Jul-2017 09:54:32.000
If you want to use the new datetime objects, the input format has a slight different syntax but the operation is roughly the same:
%% get date in [datetime] objects
formatIn = 'dd-MMM -yyyy HH:mm:ss.SSS' ;
t = datetime(time,'InputFormat',formatIn)
t =
04-Jul-2017 09:54:31
04-Jul-2017 09:54:32
Although the MATLAB console display t in human readable format, t is now a datetime object. Check the documentation if you want to use this.
Replace only two white-spaces after a month and preceding a dash (-):
>> date = '04-July -2017 09:54:30.000';
>> regexprep(date, '(\w) -', '$1-')
ans =
'04-July-2017 09:54:30.000'

Access custom date/time format and input mask error

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

Zend_Date() is not giving the correct/expected result

Zend_Date function returns the value "Feb 10, 2012" . It supposed to return "Oct 2, 2012". If a give the day greater than 12 than it returns the currect output. I don't know what I was missing. Please help me.
Thanks
If your format is YYYY-MM-DD (2012-10-02), try this code:
$str = '2012-10-02';
$date = new Zend_Date($str, Zend_Date::YEAR . '-' . Zend_Date::MONTH . '-' . Zend_Date::DAY);
echo $date->toString();
What is the code you are using to call the function?
Different countries use different date formats. Some use dd/mm/yyyy others use mm/dd/yyyy. The order you are passing in the day and month are different from the way the function expects to be called.
Try reversing the month and day before you call the function.

Changing date format to "%d/%m/%Y"

Would like to change the date format. My data frame is shown below and would like to change all the date formats to "%d/%m/%Y".
df:
id bdate wdate ddate
1 09/09/09 12/10/09 2009-09-27
df$ddate <- format(as.Date(df$ddate), "%d/%m/%Y")
df$ddate<-strftime(df$ddate,"%d/%m/%Y")
df$bdate<-strftime(strptime(df$bdate,"%d/%m/%y"),"%d/%m/%Y")
df$wdate<-strftime(strptime(df$wdate,"%d/%m/%y"),"%d/%m/%Y")
Default R action is to treat strings as factors. Of course, an individual setup may differ from defaults. It's a good practice to change variable values to character, and then convert it to date. I often use chron package - it's nice, simple and what matters the most, it does the job. Only downside of this package lays in time zone handling.
If you don't have chron installed, do:
install.packages("chron")
# load it
library(chron)
# make dummy data
bdate <- c("09/09/09", "12/05/10", "23/2/09")
wdate <- c("12/10/09", "05/01/07", "19/7/07")
ddate <- c("2009-09-27", "2007-05-18", "2009-09-02")
# notice the last argument, it will not allow creation of factors!
dtf <- data.frame(id = 1:3, bdate, wdate, ddate, stringsAsFactors = FALSE)
# since we have characters, we can do:
foo <- transform(dtf, bdate = chron(bdate, format = "d/m/Y"), wdate = chron(wdate, format = "d/m/Y"), ddate = chron(ddate, format = "y-m-d"))
# check the classes
sapply(foo, class)
# $id
# [1] "integer"
# $bdate
# [1] "dates" "times"
# $wdate
# [1] "dates" "times"
# $ddate
# [1] "dates" "times"
C'est ca... it should do the trick...

How to print date in the format of mm/dd/yyyy in VB

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")