首页 > 解决方案 > 需要帮助在反应本机应用程序中获取希伯来语日期

问题描述

一般来说,我对原生和 javascript 的反应还是很陌生,但我正在尝试学习如何使用它制作应用程序。我希望能够在我的应用程序中显示当前的希伯来语日期。希伯来历是一种独特的阴阳历,因此获取希伯来日期不能是简单的公历日期本地化。似乎有多个 js 依赖项可以为我提供希伯来语日期(到目前为止,我已经尝试过 hebcal 和 hedate),但没有一个有效。我认为 hebcal 只是与本机反应不兼容,但使用 heDate 我得到一个错误TypeError: Object is not a constructor (evaluating 'new heDate()')。我可以解决这个问题吗?如果没有,我将如何从网站上提取它?

这是我的代码:

import { setStatusBarBackgroundColor } from "expo-status-bar";
import React, { Component } from "react";
import { View, StyleSheet, Text } from "react-native";

var heDate = require("he-date");
var d = new heDate();
var date = d.getDate();

const Zmanim = () => {
  return (
    <View style={styles.container}>
      <Text style={styles.text}>{date}</Text>
    </View>
  );
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: "center",
    alignItems: "center",
    backgroundColor: "#487dc7",
  },

  text: {
    fontSize: 20,
    fontWeight: "bold",
    color: "#fff",
  },
});

export default Zmanim;

标签: javascriptandroidreact-nativedate

解决方案


图书馆“希伯来日期”似乎适用于计算。https://www.npmjs.com/package/hebrew-date

import React from 'react';
import {Text, View} from 'react-native';
import hebrewDate from 'hebrew-date';

export default function App() {

    const date = hebrewDate(new Date())

    return (
        <View>
            <Text>{date.date} {date.month_name} {date.year}</Text>
        </View>
    );
}

今天的输出(2021 年 5 月 16 日公历)= 5 Sivan 5781


推荐阅读