Time format
time format (protocol) | describe |
---|---|
gg | The period or era. |
y | The year without the era. Does not have leading zeros. |
yy | The year without the era. with leading zeros. |
yyyy | Four-digit year containing the era. |
M | month number. Single-digit months have no leading zeros. |
MM | month number. Single-digit months have a leading zero. |
MMM | The abbreviated name of the month, as defined in AbbreviatedMonthNames. |
MMMM | The full name of the month, as defined in MonthNames. |
d | the day of the month. Single-digit dates have no leading zeros. |
dd | the day of the month. Single-digit dates have a leading zero. |
ddd | The abbreviated name of the day of the week, as defined in AbbreviatedDayNames. |
dddd | The full name of the day of the week, as defined in DayNames. |
h | Hours on a 12-hour clock. Single-digit hours have no leading zeros. |
hh | Hours on a 12-hour clock. Single-digit hours have leading zeros. |
H | The hour in 24-hour format. Single-digit hours have no leading zeros. |
HH | The hour in 24-hour format. Single-digit hours have leading zeros. |
m | minute. Single-digit minutes without leading zeros. |
mm | minute. Single-digit minutes have a leading zero. |
s | Second. Single-digit seconds without leading zeros. |
ss | Second. Single-digit seconds have a leading zero. |
f | Seconds have a decimal precision of one digit. The remaining digits are truncated. |
String to time format conversion
Python
String to time format:
import datetime
str_time = '2022-01-01 12:00:00'
time_obj = datetime.datetime.strptime(str_time, '%Y-%m-%d %H:%M:%S')
Convert time format to string:
time_obj = datetime.datetime.now()
str_time = time_obj.strftime('%Y-%m-%d %H:%M:%S')
Java
String to time format:
import java.text.SimpleDateFormat;
import java.util.Date;
String str_time = "2022-01-01 12:00:00";
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date time_obj = formatter.parse(str_time);
Convert time format to string:
Date time_obj = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String str_time = formatter.format(time_obj);
C++
String to time format:
#include <iostream>
#include <ctime>
#include <string>
std::string str_time = "2022-01-01 12:00:00";
struct tm time_obj;
strptime(str_time.c_str(), "%Y-%m-%d %H:%M:%S", &time_obj);
time_t timestamp = mktime(&time_obj);
Convert time format to string:
time_t timestamp = time(NULL);
struct tm time_obj = *localtime(×tamp);
char str_time[20];
strftime(str_time, sizeof(str_time), "%Y-%m-%d %H:%M:%S", &time_obj);
std::string result(str_time);
Kotlin
String to time format:
fun main() {
val timeString = "2023-03-02T12:34:56"
val pattern = "yyyy-MM-dd'T'HH:mm:ss"
val formatter = java.time.format.DateTimeFormatter.ofPattern(pattern)
val dateTime = java.time.LocalDateTime.parse(timeString, formatter)
println(dateTime)
}
Convert time format to string:
import java.time.LocalDate
import java.time.format.DateTimeFormatter
fun main(args: Array<String>) {
// Format y-M-d or yyyy-MM-d
val string = "2017-07-25"
val date = LocalDate.parse(string, DateTimeFormatter.ISO_DATE)
println(date)
}
Shell
date to string
date +%F #输出格式 YYYY-MM-DD
date +'%F %H:%m:%S' #输出格式 YYYY-MM-DD HH:mm:ss
string to date
date -d '2016-12-27'
date -d '2016-12-27' +%s #转成时间戳
C
TIME ToTimeStamp(string strTime)
{
tm _tm;
int year, month, day, hour, minute,second;
sscanf(strTime.c_str(), "%d-%d-%d %d:%d:%d", &year, &month, &day, &hour, &minute, &second);
_tm.tm_year = year - 1900;
_tm.tm_mon = month - 1;
_tm.tm_mday = day;
_tm.tm_hour = hour;
_tm.tm_min = minute;
_tm.tm_sec = second;
_tm.tm_isdst = 0;
time_t t = mktime(&_tm);
return t;
}
C#
date to string
DateTimeFormatInfo dfInfo=new DateTimeFormatInfo();
dfInfo.ShortDatePattern = "yyyy/MM/dd hh:mm:ss:ffff";
DateTime dt = Convert.ToDateTime("2019/07/01 18:18:18:1818", dfInfo);
string dateString = dt.ToString();
//或者
dateString = dt.ToString("yyyy-MM-dd HH:mm:ss");
string to date
string str=yyyy-MM-dd hh:mm:ss;
Convert.ToDateTime(str);
//例如
string dateString = "20190701 18:18:18:1818";
DateTime dt = DateTime.ParseExact(dateString, "yyyyMMddHHmmssffff", CultureInfo.CurrentCulture);
DateTime.ParseExact(dateString, "yyyyMMddHHmmssffff", CultureInfo.InvariantCulture);
javascript
date to string
function dateToString (date){
var year = date.getFullYear(); var month =(date.getMonth() + 1).toString(); var day = (date.getDate()).toString();
if (month.length == 1) {
month = "0" + month;
}
if (day.length == 1) {
day = "0" + day;
}
var dateTime = year + "-" + month + "-" + day;
return dateTime;
}
string to date
function stringToDate (dateStr,separator){
if (!separator){
separator= "-" ;
}
var dateArr = dateStr.split(separator);var year = parseInt(dateArr[0]);var month;
if (dateArr[1].indexOf( "0" ) == 0){
month = parseInt(dateArr[1].substring(1));
} else {
month = parseInt(dateArr[1]);
}
var day = parseInt(dateArr[2]);
var date = new Date(year,month -1,day);
return date;
}
Or directly annotate the parameters
@DateTimeFormat(pattern = "yyyy-MM-dd")
@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd")
timestamp
A timestamp is a number in seconds representing the time since January 1, 1970 (UTC time).
To convert timestamps to other time formats, you can use built-in functions or libraries in various programming languages.
For example, in Python, you can use the datetime function in the datetime module to convert a timestamp to a time string.
from datetime
import datetimetimestamp = 1609459200
# 转换为本地时间
dt_obj = datetime.fromtimestamp(timestamp)
# 转换为指定的格式
time_str = dt_obj.strftime('%Y-%m-%d %H:%M:%S')print(time_str)
# Output: "2021-01-01 00:00:00"
Note that the timestamp is based on UTC time. If you need to convert it to local time, you need to use the fromtimestamp function instead of the utcfromtimestamp function.