简体   繁体   English

将 isoDatetime 转换为 dateTime 格式

[英]convert isoDatetime to dateTime format

Date d = new Date()
iso = d.toISOString()

it gives = 2019-10-28T11:23:52.384Z它给出了 = 2019-10-28T11:23:52.384Z

but I want in 2019-10-28T01:12 (YYYY-MM-DD T HH:MM AM/PM) only date time with AM/PM但我只希望在2019-10-28T01:12 (YYYY-MM-DD T HH:MM AM/PM)日期时间与 AM/PM

because in mobile view date time getting iso format.因为在移动视图中日期时间获取 iso 格式。

在此处输入图像描述

Use moment.js使用moment.js

moment().format("YYYY-MM-DDThh:mm, A");

Or use Constructor或者使用构造函数

 let d = new Date()
 let iso = moment(d).format("YYYY-MM-DDThh:mm, A");

got: "2019-10-28T08:18, PM"得到: "2019-10-28T08:18, PM"

notes:笔记:

2019-10-28T11:23:52.384Z

is ISO8601 format check wiki or iso.org是 ISO8601 格式检查wikiiso.org

You can use a javascript library known as momentjs您可以使用称为momentjs的 javascript 库

Using their format function you can manipulate the date format to your liking.使用他们的format function 您可以根据自己的喜好操作日期格式。

for your case, it should be对于您的情况,应该是

import moment from "moment"

let date = new Date();
let yyyymmdd = moment(date).format("YYYY-MM-DD");
let hhmm = moment(date).format("LT");
let ans = yyyymmdd + " T " + hhmm;

output: output:

2019-10-28 T 6:28 PM

codepen密码笔

Is this useful?这有用吗?

 const pad = num => ("0" + num).slice(-2) const d = new Date(2019, 9, 28, 3, 1, 1) const iso = d.toISOString() let [date, time] = iso.split("T") const [_, hh, mm, ss, ampm] = new Date(iso).toLocaleTimeString('en-US').match(/(\d{1,2}):(\d{2}):(\d{2}) (\w{2})/) console.log(date + "T" + pad(hh) + ":" + pad(mm) + " " + ampm)

You can strip out the date components and then piece them back in the order you would like.您可以去掉日期组件,然后按照您想要的顺序将它们拼凑回来。

Hours and meridian will need to be adjusted.需要调整时间和经络。

 const DateComponenents = (() => { const fields = ['YYYY', 'MM', 'DD', 'HH', 'mm', 'ss', 'SSS']; const pattern = /^(\d{4})\-(\d{2})\-(\d{2})T(\d{2}):(\d{2}):(\d{2})\.(\d{3})Z$/; return (date) => { return date.toISOString().match(pattern).slice(1).reduce((r, v, i) => { return Object.assign(r, { [fields[i]]: v }); }, {}); } })(); function formatDate(date) { let c = DateComponenents(date); // Extract the componenents let hours = parseInt(c['HH'], 10); // Grab the 24-hour piece let meridiem = hours < 12? 'AM': 'PM'; // Determine AM or PM hours %= 12; // Modular 0 - 12 if (hours === 0) hours = 12; // Shift 0am to 12am hours = ('00' + hours).substr(-2); // Pad the hours return `${c['YYYY']}-${c['MM']}-${c['DD']} T ${hours}:${c['mm']} ${meridiem}` } console.log(formatDate(new Date()));
 .as-console-wrapper { top: 0; max-height: 100%;important; }

Here is a more dynamic version that supports custom formatting...这是一个支持自定义格式的更动态的版本...

 const DateComponenents = (() => { const fields = ['YYYY', 'MM', 'DD', 'HH', 'mm', 'ss', 'SSS']; const pattern = /^(\d{4})\-(\d{2})\-(\d{2})T(\d{2}):(\d{2}):(\d{2})\.(\d{3})Z$/; const applyRest = (c) => { let hours = parseInt(c['HH'], 10); let meridiem = hours < 12? 'AM': 'PM'; hours %= 12; hours = ('00' + (hours === 0? 12: hours)).substr(-2); return Object.assign(c, { 'hh': hours, 'A': meridiem }); } return (date) => { return applyRest(date.toISOString().match(pattern).slice(1).reduce((r, v, i) => { return Object.assign(r, { [fields[i]]: v }); }, {})); }; })(); function formatDate(date, format) { return (c => format.replace(/\w+/g, key => c[key] || key))(DateComponenents(date)); } console.log(formatDate(new Date(), 'YYYY-MM-DD T hh:mm A'));
 .as-console-wrapper { top: 0; max-height: 100%;important; }

I suggest you use the js standard library Intl.DateTimeFormat API's .我建议你使用 js 标准库Intl.DateTimeFormat API 的. They give you a lot of control and are built-in.它们为您提供了很多控制权并且是内置的。

They are new-ish but are supported by all browsers other then MSIE.它们是新的,但除 MSIE 之外的所有浏览器都支持。

const options = {
      year: 'numeric',
      month: 'numeric',
      day: 'numeric',
      hour: '2-digit',
      minute: 'numeric',
      hour12: true  // Gives you the AM PM
    };
// Note that the 'second' is omitted
// See 'timeZone' and 'timeZoneName' options if you want to control those.

const dateString = new Intl.DateTimeFormat( 'en-ca', options ).format( new Date() );

Note that I specified Canadian English for the locale because that is one of the many countries that has adopted ISO 8601 as their recommended format.请注意,我为语言环境指定了加拿大英语,因为这是采用 ISO 8601 作为推荐格式的众多国家之一。 The only drawback of 'en-ca' is that it adds a comma but you can just use string.replace to swap that for the 'T'. 'en-ca' 的唯一缺点是它添加了一个逗号,但您可以使用 string.replace 将其交换为 'T'。

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

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