c++ mysql ctime_CTime::Format

CTime ct = CTime::GetCurrentTime();

CString str = ct.Format("%Y-%m_%d %H-%M-%S");

输出为:str="2006-04-23 15-21-30"

%a

Abbreviated weekday name

%A

Full weekday name

%b

Abbreviated month name

%B

Full month name

%c

Date and time representation appropriate for locale

%d

Day of month as decimal number (01 – 31)

%H

Hour in 24-hour format (00 – 23)

%I

Hour in 12-hour format (01 – 12)

%j

Day of year as decimal number (001 – 366)

%m

Month as decimal number (01 – 12)

%M

Minute as decimal number (00 – 59)

%p

Current locale's A.M./P.M. indicator for 12-hour clock

%S

Second as decimal number (00 – 59)

%U

Week of year as decimal number, with Sunday as first day of week (00 – 53)

%w

Weekday as decimal number (0 – 6; Sunday is 0)

%W

Week of year as decimal number, with Monday as first day of week (00 – 53)

%x

Date representation for current locale

%X

Time representation for current locale

%y

Year without century, as decimal number (00 – 99)

%Y

Year with century, as decimal number

%z, %Z

Either the time-zone name or time zone abbreviation, depending on registry settings; no characters if time zone is unknown

%%

Percent sign

// Example for CTime::Format and CTime::FormatGmt

CTime t( 1999, 3, 19, 22, 15, 0 );

// 10:15 PM March 19, 1999

CString s = t.Format( "%A, %B %d, %Y" );

ATLASSERT( s == "Friday, March 19, 1999" );

### 将毫秒级时间戳转换为年月日格式 要将1741915401000这样的毫秒级时间戳转换为年月日格式,可以通过多种方法实现。以下是几种常见的技术栈下的解决方案。 #### 方法一:C/C++ 实现 在C/C++中,可以利用标准库中的`time.h`头文件来完成这一操作。具体来说,先将毫秒级时间戳除以1000得到秒级时间戳,再通过`gmtime`或`localtime`将其转化为结构体形式的时间数据,最后提取所需的年、月、日部分[^1]。 ```cpp #include <iostream> #include <ctime> int main() { long long timestamp_ms = 1741915401000; time_t timestamp_s = timestamp_ms / 1000; // 转换为秒级时间戳 struct tm* local_time = localtime(&timestamp_s); int year = local_time->tm_year + 1900; // 年份从1900年开始计数 int month = local_time->tm_mon + 1; // 月份从0开始计数 int day = local_time->tm_mday; std::cout << "Date: " << year << "-" << month << "-" << day << std::endl; } ``` #### 方法二:MySQL 中的实现 如果是在数据库查询场景下处理时间戳,则可以直接使用SQL语句配合内置函数完成转换工作。例如,在MySQL里有专门用于格式化日期显示效果的`DATE_FORMAT()`功能[^2]: ```sql SELECT DATE_FORMAT(FROM_UNIXTIME(UNIX_TIMESTAMP('1970-01-01') + FLOOR(1741915401000/1000)), '%Y-%m-%d'); ``` 这里需要注意的是原始输入单位应调整至秒钟级别后再参与运算过程。 #### 方法三:HTML/Javascript 前端实现 对于Web开发环境而言,JavaScript提供了强大的Date对象支持各种复杂的日期计算需求。下面展示了一种简单易懂的方式来进行相应变换[^3]: ```javascript function formatDate(timestampMs){ let dateObj=new Date(Number(timestampMs)); const options={year:'numeric',month:'2-digit',day:'2-digit'}; return new Intl.DateTimeFormat('en-US',options).format(dateObj); } console.log(formatDate(1741915401000)); // 输出类似于 "MM/DD/YYYY" ``` 或者采用正则表达式替换法手动构建目标样式字符串: ```javascript var ts=1741915401000; var d=new Date(ts); document.write(d.getFullYear()+"-"+((d.getMonth()+1)<10?"0":"")+(d.getMonth()+1)+"-"+(d.getDate()<10?"0":"")+d.getDate()); ``` 以上就是不同平台环境下针对同一问题所提供的解答方案概览。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值