如何处理 Javascript 中的日期格式?

发布于 2025-01-09 08:25:16 字数 1734 浏览 0 评论 0原文

我在弄清楚 js 中重新格式化/转换日期字段的正确方法时遇到问题。

我正在尝试使用 Kafka ODF 流送器和接收器连接器以及中间的 js 创建一个报告来处理来自输入 EDR 文件的转换。

示例 O/P 报告

MSISDN,DeleteReason,DeleteTime
12554564364661,3,2022-02-21T14:08:39.795Z

预期报告格式

MSISDN,DeleteReason,DeleteTime
12554564364661,3,20220221140839

注意:- 希望将删除时间字段转换为不同的格式“YYYYMMDDhhmiss”,目前为 YYYY-MM-DDTHH:MM:SS。米兹。

接收器连接器中的转换部分

transforms: valueTransform
transforms.valueTransform.type: "com.openet.odf.transformer.ValueTransform"
transforms.valueTransform.script: "/opt/kafka/external-configuration/sink-hookpoint/valueTransform.js"

eventTimestamp[或DeleteTime] 是在架构中声明为字符串的传入日期字段。例如: 2022-02-21T14:08:39.795Z

Js Script

  valueTransform.js: |
    function onValueTransform(_schema, _value) {
        var SchemaAndValue = Java.type('org.apache.kafka.connect.data.SchemaAndValue')
        var Date = Java.type('java.util.Date')
        
        var d = new Date.parse(_value.get("eventTimestamp"))
        var year = d.getFullYear()
        var month = ('0' + (d.getMonth() + 1)).slice(-2)
        var day = ('0' + d.getDate()).slice(-2)
        var hours = ('0' + d.getHours()).slice(-2)
        var minutes = ('0' + d.getMinutes()).slice(-2)
        var seconds = ('0' + d.getSeconds()).slice(-2)
        var modified_delete_time = year + month + day + hours + minutes + seconds        

        _value.put("eventTimestamp",modified_delete_time)
        
        return new SchemaAndValue(_schema, _value)
        }

我尝试过使用 Date、SimpleDateFormat 等,但它们都不起作用,并且每个都出现不同的错误试图。

I am having issues figuring out the correct way to reformat/convert date fields in the js.

I am trying to create a report using the Kafka ODF streamer and sink connector and a js in between to handle transform, from an input EDR file.

Sample O/P report

MSISDN,DeleteReason,DeleteTime
12554564364661,3,2022-02-21T14:08:39.795Z

Expected report format

MSISDN,DeleteReason,DeleteTime
12554564364661,3,20220221140839

Note:- Want to transform the DeleteTime field to a different format "YYYYMMDDhhmiss", currently YYYY-MM-DDTHH:MM:SS.MISZ .

Transform section in the sink connector

transforms: valueTransform
transforms.valueTransform.type: "com.openet.odf.transformer.ValueTransform"
transforms.valueTransform.script: "/opt/kafka/external-configuration/sink-hookpoint/valueTransform.js"

eventTimestamp[or DeleteTime] is an incoming date field declared as a string in the schema. ex: 2022-02-21T14:08:39.795Z

Js Script

  valueTransform.js: |
    function onValueTransform(_schema, _value) {
        var SchemaAndValue = Java.type('org.apache.kafka.connect.data.SchemaAndValue')
        var Date = Java.type('java.util.Date')
        
        var d = new Date.parse(_value.get("eventTimestamp"))
        var year = d.getFullYear()
        var month = ('0' + (d.getMonth() + 1)).slice(-2)
        var day = ('0' + d.getDate()).slice(-2)
        var hours = ('0' + d.getHours()).slice(-2)
        var minutes = ('0' + d.getMinutes()).slice(-2)
        var seconds = ('0' + d.getSeconds()).slice(-2)
        var modified_delete_time = year + month + day + hours + minutes + seconds        

        _value.put("eventTimestamp",modified_delete_time)
        
        return new SchemaAndValue(_schema, _value)
        }

I've tried using Date, SimpleDateFormat etc, but none of it is working and getting different errors with each attempt.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

给不了的爱 2025-01-16 08:25:16

没有任何合理的内置内容。您可以在 Javascript 中将 Moment Timezone 用于 Moment.js

var actualDateTime = moment("2022-02-21T14:08:39.795Z");
var f = "YYYYMMDDhhmmss";
console.log(actualDateTime.format(f)) // By passing format into the format function to get it done

There's nothing reasonable built-in. You can use Moment Timezone for Moment.js in Javascript.

var actualDateTime = moment("2022-02-21T14:08:39.795Z");
var f = "YYYYMMDDhhmmss";
console.log(actualDateTime.format(f)) // By passing format into the format function to get it done
紧拥背影 2025-01-16 08:25:16

在大多数情况下,处理日期可能是一个棘手的过程。

我会考虑选择一个 Dates 库,例如 https://www.npmjs.com/package/luxon< /a>.

我的第一个选择是 Moment.js,但该项目已被弃用。

使用 Luxon,您可以依赖解析和格式化实用程序,如下所示:

DateTime.fromISO('2022-02-23T08:30:00').toFormat('yyyyMMddhhmmss')

In most of the cases, dealing with Dates may be a tricky process.

I would consider choosing a Dates library, for instance https://www.npmjs.com/package/luxon.

My first choise would be Moment.js, but the project is deprecated.

Using Luxon, you can rely on parsing and formatting utilities, something like that:

DateTime.fromISO('2022-02-23T08:30:00').toFormat('yyyyMMddhhmmss')
氛圍 2025-01-16 08:25:16

谢谢,论坛。

我能够使用 Calendar 和 SimpleDateFormat 来达到预期的结果。解决方案如下

valueTransform.js: |
    function onValueTransform(_schema, _value) {
        var SchemaAndValue = Java.type('org.apache.kafka.connect.data.SchemaAndValue')
        var Date = Java.type('java.util.Date')
        var SimpleDateFormat = Java.type('java.text.SimpleDateFormat')
        var Calendar = Java.type('java.util.Calendar')        
                
        var sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss") 
        var EventDate = sdf.parse(_value.get("eventTimestamp"))
        sdf=new SimpleDateFormat("yyyyMMddHHmmss")
        var cal = Calendar.getInstance()
        cal.setTime(EventDate)
        EventDate = cal.getTime()
        var formattedDate = sdf.format(EventDate)
        
        _value.put("eventTimestamp",formattedDate)
        
        return new SchemaAndValue(_schema, _value)
        }

Thanks, Forum.

I was able to use Calendar and SimpleDateFormat to achieve the desired results. Solution below

valueTransform.js: |
    function onValueTransform(_schema, _value) {
        var SchemaAndValue = Java.type('org.apache.kafka.connect.data.SchemaAndValue')
        var Date = Java.type('java.util.Date')
        var SimpleDateFormat = Java.type('java.text.SimpleDateFormat')
        var Calendar = Java.type('java.util.Calendar')        
                
        var sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss") 
        var EventDate = sdf.parse(_value.get("eventTimestamp"))
        sdf=new SimpleDateFormat("yyyyMMddHHmmss")
        var cal = Calendar.getInstance()
        cal.setTime(EventDate)
        EventDate = cal.getTime()
        var formattedDate = sdf.format(EventDate)
        
        _value.put("eventTimestamp",formattedDate)
        
        return new SchemaAndValue(_schema, _value)
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文