Excel宏:如何在“中”获取时间戳;yyyy MM dd hh:MM:ss“;格式?
我正在使用Excel宏中的Excel宏:如何在“中”获取时间戳;yyyy MM dd hh:MM:ss“;格式?,excel,vba,Excel,Vba,我正在使用Excel宏中的DateTime.Now显示当前时间戳 它以“dd-MM-yyy-hh:MM:ss”格式显示时间戳 相反,如何以“yyy-MM-dd hh:MM:ss”格式获取时间戳?尝试:格式(现在(),“yyy-MM-dd hh:MM:ss”)使用格式功能 Format(Date, "yyyy-mm-dd hh:MM:ss") DateTime。现在返回数据类型为Date的值。日期变量根据计算机上设置的短日期格式和时间格式显示日期 它们可以被格式化为字符串,以便通过其他答案中提到
DateTime.Now
显示当前时间戳
它以“dd-MM-yyy-hh:MM:ss”格式显示时间戳
相反,如何以“yyy-MM-dd hh:MM:ss”格式获取时间戳?尝试:
格式(现在(),“yyy-MM-dd hh:MM:ss”)
使用格式功能
Format(Date, "yyyy-mm-dd hh:MM:ss")
DateTime。现在
返回数据类型为Date
的值。日期变量根据计算机上设置的短日期格式和时间格式显示日期
它们可以被格式化为字符串,以便通过其他答案中提到的format
函数以任何有效的日期格式显示
Format(DateTime.Now, "yyyy-MM-dd hh:mm:ss")
如果代码的某些用户具有不同的语言设置,则通过单击类型下的海关类别,将此格式复制并粘贴到格式单元格中。因此,我使用以下代码以“YYYYMMDD hhMMss”格式给出时间戳,而不考虑语言
Function TimeStamp()
Dim iNow
Dim d(1 To 6)
Dim i As Integer
iNow = Now
d(1) = Year(iNow)
d(2) = Month(iNow)
d(3) = Day(iNow)
d(4) = Hour(iNow)
d(5) = Minute(iNow)
d(6) = Second(iNow)
For i = 1 To 6
If d(i) < 10 Then TimeStamp = TimeStamp & "0"
TimeStamp = TimeStamp & d(i)
If i = 3 Then TimeStamp = TimeStamp & " "
Next i
End Function
函数时间戳()
迪米诺
尺寸d(1至6)
作为整数的Dim i
iNow=现在
d(1)=年(iNow)
d(2)=月份(iNow)
d(3)=天(iNow)
d(4)=小时(iNow)
d(5)=分钟(iNow)
d(6)=秒(iNow)
对于i=1到6
如果d(i)<10,则时间戳=时间戳&“0”
时间戳=时间戳&d(i)
如果i=3,则TimeStamp=TimeStamp&“
接下来我
端函数
这对我最有效:
Cells(partcount + 5, "N").Value = Date + Time
Cells(partcount + 5, "N").NumberFormat = "mm/dd/yy hh:mm:ss AM/PM"
它可以像这样简单,选择你想要的位置,在这种情况下我选择D3
Sheets("Put your Sheet's name here").Range("D3") = Now
例如,我的工作表被称为Sources
Sheets("Sources").Range("D3") = Now
时间戳在保存工作簿路径时,“
:
”需要更改。我使用了“:
”->“
”,这意味着我需要将扩展添加回“xlsx
”
不要工作<代码>日期仅包含日历日期,但小时、分钟和秒固定为0:00:00!您必须改用
Now()
。。
Sheets("Sources").Range("D3") = Now
wb(x).SaveAs ThisWorkbook.Path & "\" & unique(x) & " - " & Format(Now(), "mm-dd-yy, hh.mm.ss") & ".xlsx"