程序怎么写年月日

时间:2025-01-29 04:19:28 单机游戏

获取和打印年月日的方法取决于你使用的编程语言。以下是几种常见编程语言中获取和打印年月日的方法:

Python

```python

import datetime

current_date = datetime.date.today()

print(current_date)

```

Java

```java

import java.time.LocalDate;

LocalDate currentDate = LocalDate.now();

System.out.println(currentDate);

```

C

```csharp

using System;

DateTime currentDate = DateTime.Today;

Console.WriteLine(currentDate);

```

JavaScript

```javascript

let currentDate = new Date();

console.log(currentDate);

```

C++

```cpp

include

include

int main() {

time_t now = time(0);

tm* currentDate = localtime(&now);

std::cout << currentDate->tm_year + 1900 << "-"

<< currentDate->tm_mon + 1 << "-"

<< currentDate->tm_mday << std::endl;

return 0;

}

```

PHP

```php

$currentDate = date("Y-m-d");

echo $currentDate;

```

C语言

```c

include

int main() {

int year, month, day;

printf("请输入年份: ");

scanf("%d", &year);

printf("请输入月份: ");

scanf("%d", &month);

printf("请输入日期: ");

scanf("%d", &day);

printf("输入的年月日为: %d年%d月%d日", year, month, day);

return 0;

}

```

输出格式化日期

如果你想要以特定的格式输出日期,例如“May 21, 2021”,可以使用以下代码:

Python

```python

from datetime import datetime

current_date = datetime.date.today()

formatted_date = current_date.strftime("%B %d, %Y")

print(formatted_date)

```

Java

```java

import java.time.LocalDate;

import java.time.format.DateTimeFormatter;

LocalDate currentDate = LocalDate.now();

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMMM d, yyyy");

String formattedDate = currentDate.format(formatter);

System.out.println(formattedDate);

```

C

```csharp

using System;

DateTime currentDate = DateTime.Today;

string formattedDate = currentDate.ToString("MMMM dd, yyyy");

Console.WriteLine(formattedDate);

```

JavaScript

```javascript

let currentDate = new Date();

let formattedDate = currentDate.toLocaleDateString("en-US", { month: "long", day: "numeric", year: "numeric" });

console.log(formattedDate);

```

这些示例展示了如何在不同的编程语言中获取和格式化当前日期。你可以根据需要选择合适的编程语言和日期格式。