Javascript(ES6)日期为字符串格式

时间:2019-05-28 10:53:47

标签: javascript ecmascript-6

在Javascript中,我有14:51:02 at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83) 14:51:02 at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:548) 14:51:02 at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:212) 14:51:02 at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:130) 14:51:02 at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:181) 14:51:02 at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:168) 14:51:02 at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:123) 14:51:02 at classes.Base.setUp(Base.java:71) 14:51:02 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 14:51:02 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 14:51:02 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 14:51:02 at java.lang.reflect.Method.invoke(Method.java:498) 14:51:02 at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84) 14:51:02 at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:564) 14:51:02 at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:213) 14:51:02 at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:138) 14:51:02 at org.testng.internal.TestMethodWorker.invokeBeforeClassMethods(TestMethodWorker.java:175) 14:51:02 at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:107) 14:51:02 at org.testng.TestRunner.privateRun(TestRunner.java:767) 14:51:02 at org.testng.TestRunner.run(TestRunner.java:617) 14:51:02 at org.testng.SuiteRunner.runTest(SuiteRunner.java:334) 14:51:02 at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329) 14:51:02 at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291) 14:51:02 at org.testng.SuiteRunner.run(SuiteRunner.java:240) 14:51:02 at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) 14:51:02 at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) 14:51:02 at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224) 14:51:02 at org.testng.TestNG.runSuitesLocally(TestNG.java:1149) 14:51:02 at org.testng.TestNG.run(TestNG.java:1057) 14:51:02 at org.apache.maven.surefire.testng.TestNGExecutor.run(TestNGExecutor.java:77) 14:51:02 at org.apache.maven.surefire.testng.TestNGDirectoryTestSuite.execute(TestNGDirectoryTestSuite.java:110) 14:51:02 at org.apache.maven.surefire.testng.TestNGProvider.invoke(TestNGProvider.java:106) 14:51:02 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 14:51:02 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 14:51:02 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 14:51:02 at java.lang.reflect.Method.invoke(Method.java:498) 14:51:02 at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189) 14:51:02 at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165) 14:51:02 at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85) 14:51:02 at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115) 14:51:02 at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75) 14:51:02 14:51:02 14:51:02 Results : 14:51:02 14:51:02 Failed tests: setUp(Testcases.TestCase): unknown error: Chrome failed to start: exited abnormally(..) 14:51:02 对象,我需要将其转换为格式为Datedd.mm.yyyy的字符串。如何使用JavaScript格式化日期?

3 个答案:

答案 0 :(得分:3)

看起来像德文语言环境格式,因此可以将Date.toLocaleDateString()Date.toLocaleTimeString()与德文语言环境设置(de-DE)一起使用:

const date = new Date();

const dateString = date.toLocaleDateString('de-DE');
const timeString = date.toLocaleTimeString('de-DE');

const string = `${dateString} ${timeString}`;

console.log(string);

答案 1 :(得分:1)

您可以根据需要使用momentjs库。

constructor(  ) { 
  let now = moment().format("YYYY-MM-DD HH:mm:ss");
  let now2 = moment().format("DD.MM.YYYY");
     this.format1 = now;
     this.format2 = now2;
     console.log(now);

    this.date = moment(this.format1, "YYYY-MM-DD HH:mm:ss");

  }

https://stackblitz.com/edit/momentjs-format-date?file=app/app.component.ts

答案 2 :(得分:0)

您可以使用Date对象的方法来构建日期字符串:

const now = new Date();

const formattedDate = now.getDate() + "." + ('0' + (now.getMonth()+1)).slice(-2) + "." + now.getFullYear();

const formattedDateTime = formattedDate + " " + now.getHours() + ":" + now.getMinutes() + ":" + now.getSeconds();

console.log(formattedDate); // 28.05.2019

console.log(formattedDateTime); // 28.05.2019 13:19:43