vba Excel 宏:如何以“yyyy-MM-dd hh:mm:ss”格式获取时间戳?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5457069/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 11:17:35  来源:igfitidea点击:

Excel Macro : How can I get the timestamp in "yyyy-MM-dd hh:mm:ss" format?

excelvba

提问by Parth Bhatt

I am using DateTime.Nowin my Excel Macro to show the current timestamp.

DateTime.Now在 Excel 宏中使用来显示当前时间戳。

It shows timestamp in "dd-MM-yyyy hh:mm:ss" format.

它以“dd-MM-yyyy hh:mm:ss”格式显示时间戳。

Instead, how can I get the timestamp in "yyyy-MM-dd hh:mm:ss" format?

相反,如何以“yyyy-MM-dd hh:mm:ss”格式获取时间戳?

回答by Mohamed Saligh

Try with: format(now(), "yyyy-MM-dd hh:mm:ss")

尝试: format(now(), "yyyy-MM-dd hh:mm:ss")

回答by chris neilsen

DateTime.Nowreturns a value of data type Date. Date variables display dates according to the short date format and time format set on your computer.

DateTime.Now返回数据类型的值Date。日期变量根据计算机上设置的短日期格式和时间格式显示日期。

They may be formatted as a string for display in any valid date format by the Formatfunction as mentioned in aother answers

它们可以被格式化为字符串,以便通过Format其他答案中提到的函数以任何有效的日期格式显示

Format(DateTime.Now, "yyyy-MM-dd hh:mm:ss")

回答by Mitch Wheat

Format(Now(), "yyyy-MM-dd hh:mm:ss")

回答by Kaisa

If some users of the code have different language settings format might not work. Thus I use the following code that gives the time stamp in format "yyymmdd hhMMss" regardless of language.

如果代码的某些用户具有不同的语言设置格式可能不起作用。因此,无论语言如何,我都使用以下代码以“yyymmdd 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

回答by user6300304

this worked best for me:

这对我最有效:

        Cells(partcount + 5, "N").Value = Date + Time
        Cells(partcount + 5, "N").NumberFormat = "mm/dd/yy hh:mm:ss AM/PM"

回答by Rafiq

Copy and paste this format yyyy-mm-dd hh:MM:ssin format cells by clicking customs category under Type

通过单击类型下的海关类别,将此格式yyyy-mm-dd hh:MM:ss复制并粘贴到格式单元格中

回答by Wizhi

Timestamp in saving workbook path, the ":" needs to be changed. I used ":" -> "." which implies that I need to add the extension back "xlsx".

保存工作簿路径中的时间戳,“ :”需要更改。我使用了“ :”->“ .”,这意味着我需要将扩展​​名添加回“ xlsx”。

wb(x).SaveAs ThisWorkbook.Path & "\" & unique(x) & " - " & Format(Now(), "mm-dd-yy, hh.mm.ss") & ".xlsx"

回答by Flori

It can work as easy as this, choose the location you want, in this case I choose D3

它可以像这样简单地工作,选择你想要的位置,在这种情况下我选择 D3

Sheets("Put your Sheet's name here").Range("D3") = Now

Example, my sheet is called Sources

例如,我的工作表被称为 Sources

Sheets("Sources").Range("D3") = Now

回答by Arnoldiusss

Use the Format function.

使用格式功能。

Format(Date, "yyyy-mm-dd hh:MM:ss")