简体   繁体   English

将日期格式从mm / dd / yy更改为dd / mm / yy

[英]Change dateformat from mm/dd/yy to dd/mm/yy

I'm using a Google Form to collect dates from users, they use a datepicker. 我正在使用Google表单收集用户的日期,他们使用日期选择器。 The format is US with mm/dd/yy (1/25/2016), but i need EU format dd/mm/yy (25/1/2016). 格式为美国,采用mm / dd / yy(1/25/2016),但我需要使用欧盟格式dd / mm / yy(25/1/2016)。

The script collects serval dates: 该脚本收集服务日期:

  var Date1 = e.values[24]; 
  var Date2 = e.values[25];
  var Date3 = e.values[26];
  var Date4 = e.values[27];  
  var Date5 = e.values[28];

How can i convert these to EU dateformat? 如何将它们转换为EU dateformat?

This is what iv'e tried so far without success: 到目前为止,这是我尝试过的未成功的事情:

1) 1)

  function myFunction() {
      var preDate = "1/25/2016";
      var postDate = preDate.Utilities.formatDate(dt, "PST", "dd/mm/yy");
      Logger.log(postDate);
    }

Error: TypeError: Cannot call method "formatDate" of undefined. 错误:类型错误:无法调用未定义的方法“ formatDate”。

2) 2)

function myFunction() {
  var preDate = "1/25/2016"; 
  var postDate = Date.parseExact(preDate ,"dd/MM/yyyy")
  Logger.log(postDate);
}

Error: TypeError: Cannot find function parseExact in object function Date() { [native code for Date.Date, arity=1] } 错误:TypeError:在对象函数Date()中找不到函数parseExact {[Date.Date的本机代码,arity = 1]}

If you have the dates in a Google Spreadsheet, you can use the Utilities.formatDate method to convert dates into another format. 如果您在Google Spreadsheet中有日期,则可以使用Utilities.formatDate方法将日期转换为另一种格式。

function convertDate() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var source = sheet.getRange("A1").getValue();
  if (typeof source === "date") {
   sheet.getRange("B1").setValue(Utilities.formatDate(dt, "PST", "dd/mm/yy"));
  }
}

An easier option would be that you select the entire column in the spreadsheet and the change the default format under the Format menu (see screenshot). 一个更简单的选择是您选择电子表格中的整个列,然后在“格式”菜单下更改默认格式(请参见屏幕截图)。

更改电子表格中的日期格式

You can use DateJS for that, like this: 您可以为此使用DateJS,如下所示:

http://code.google.com/p/datejs/wiki/APIDocumentation#parseExact http://code.google.com/p/datejs/wiki/APIDocumentation#parseExact

Date.parseExact("10/15/2004", "M/d/yyyy");  // The Date of 15-Oct-2004
Date.parse("15-Oct-2004", "d-MMM-yyyy");    // The Date of 15-Oct-2004
Date.parse("2004.10.15", "yyyy.MM.dd");     // The Date of 15-Oct-2004
Date.parseExact("10/15/2004", ["M/d/yyyy", "MMMM d, yyyy"]); // The Date of 15-Oct-2004

In your question you need that: 在您的问题中,您需要:

Date.parseExact("dateyouneed" ,"dd/MM/yyyy")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM