1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Databricks にて JavaScript の Date.prototype.toString() が生成するデフォルトの表示形の値を timestamp 型として取り込む方法

Last updated at Posted at 2025-06-23

概要

Databricks にて、 JavaScript の Date.prototype.toString() が生成するデフォルトの表示形式である 曜日 + 月 + 日付 + 年 + 時刻 + GMT±hhmm + 括弧付きタイムゾーン名(例:Sun Jun 22 2025 12:34:56 GMT+0900 (Japan Standard Time))という書式の値を timestamp 型として取り込む方法を紹介します。

image.png

処理方法

事前準備

process.env.TZ = 'Asia/Tokyo';
const d = new Date(2025, 5, 22, 12, 34, 56);
console.log(d.toString());
Sun Jun 22 2025 12:34:56 GMT+0900 (Japan Standard Time)

image.png

Databricks で timestamp 型として取り込む処理

from pyspark.sql import functions as F

df = spark.createDataFrame(
    [
        ("Sun Jun 22 2025 12:34:56 GMT+0900 (Japan Standard Time)",),
    ],
    ["timesmtap_string_from_javascript"],
)

pattern = "MMM dd yyyy HH:mm:ss 'GMT'Z"

result = df.withColumn(
    "ts",
    F.to_timestamp(
        F.regexp_replace(
            F.substring("timesmtap_string_from_javascript", 5, 100), r"\s*\(.*\)$", ""
        ),
        pattern,
    ),
)

result.printSchema()
display(result)

image.png

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?