从日期中删除时间部分

时间:2019-06-13 06:24:27

标签: javascript

let date = invoice.due_date;
    console.log(date);

输出2019-06-13 00:00:00

d = date.split(' ')[0]; //didnt work for me

如何删除时间并且只有日期。

5 个答案:

答案 0 :(得分:2)

我刚刚添加了.toLocaleDateString

toLocaleDateString()方法返回一个字符串,该字符串具有日期的日期部分的语言敏感表示。语言环境和选项参数使应用程序可以指定应使用其格式约定的语言,并允许自定义函数的行为。

let date = new Date("2019-06-13T02:00:00Z").toLocaleDateString()
console.log(date)

参考:

另一个示例: 如果您想拥有一个ISO日期,请尝试以下一项:

date = new Date('2019-06-13T02:00:00Z');
year = date.getFullYear();
month = date.getMonth() + 1;
dt = date.getDate();

if (dt < 10) {
  dt = '0' + dt;
}
if (month < 10) {
  month = '0' + month;
}

console.log(year + '-' + month + '-' + dt);

答案 1 :(得分:-1)

如果您有字符串,则拆分将起作用。

它不是字符串(例如null),或者不是字符串。

您的console.log显示一个日期字符串,因此它显然是一个Date对象。

要在任何情况下(空格或日期和时间之间带有T)获取第二部分,您都需要获取ISOString,以便能够持久获取正确的输出。

任何toLocaleString或类似内容都取决于实现和语言环境

let date = invoice.due_date.toISOString()

赞:

// Assuming a date object because your console log and the split that does not work

const invoice = {
  due_date : new Date("2019-06-13 00:00:00") // EXAMPLE date
}  

let date = invoice.due_date.toISOString(); 
console.log(date)
console.log(date.split(/[T| ]/)[0]); // take space or "T" as delimiter

答案 2 :(得分:-1)

let date = invoice.due_date;
console.log(date.getDate() + '-' + (date.getMonth()+1) + '-' + date.getFullYear());

您可以尝试这种方式。可以创建dd-MM-yyyy之类的任何格式。

建议:使用moment库进行日期格式设置。

答案 3 :(得分:-2)

您可以将日期字符串转换为Date对象:

let dataObj = new Date(date)

,然后按照此link

中的格式进行格式化

答案 4 :(得分:-2)

如果日期是字符串,那么可以

try (TextToSpeechClient textToSpeechClient = TextToSpeechClient.create()) {
        // Set the text input to be synthesized
        SynthesisInput input = SynthesisInput.newBuilder()
              .setText(strText)
              .build();

        VoiceSelectionParams voice = VoiceSelectionParams.newBuilder()
            .setLanguageCode("en-US")
            .setSsmlGender(SsmlVoiceGender.NEUTRAL)
            .build();

            // Select the type of audio file you want returned
            AudioConfig audioConfig = AudioConfig.newBuilder()
            .setAudioEncoding(AudioEncoding.MP3)
            .build();

        // Perform the text-to-speech request on the text input with the selected voice parameters and
        // audio file type
        SynthesizeSpeechResponse syntehsizeResponse = textToSpeechClient.synthesizeSpeech(input, voice,
            audioConfig);   

        // Get the audio contents from the response
        ByteString audioContents = syntehsizeResponse.getAudioContent();


Error-----------------------------------------------------------------

Caused by: javax.net.ssl.SSLHandshakeException: error:10000410:SSL routines:OPENSSL_internal:SSLV3_ALERT_HANDSHAKE_FAILURE 
        at io.grpc.netty.shaded.io.netty.handler.ssl.ReferenceCountedOpenSslEngine.shutdownWithError(ReferenceCountedOpenSslEngine.java:897)