前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用Blazor做个简单的时间戳在线转换工具

使用Blazor做个简单的时间戳在线转换工具

作者头像
沙漠尽头的狼
发布2022-03-26 09:43:53
2.7K0
发布2022-03-26 09:43:53
举报
文章被收录于专栏:Dotnet9Dotnet9

时间戳转换

时间戳转换,关键点在于双向绑定@bind-Value,就简单贴源码吧

TimestampTool.razor

代码语言:javascript
复制
@page "/timestamp"
@using BlazorComponent.I18n
@layout PublicLayout

<PageTitle>@T("TimestampToolTitle")</PageTitle>

<h2 style="margin-bottom: 10px; margin-top: 10px; text-align: center;">@T("TimestampToolDesc")</h2>

<MRow>
    @T("TimestampToolDateNow") @DateToTimestamp(DateTime.Now, TimestampKind.Seconds)
</MRow>
<MRow>
    <MTextField Label="@T("TimestampToolTimestamp")" TValue="long" @bind-Value="@_timestamp1"/>
    <MSelect @bind-Value="@_kindValue1"
             Label="@T("TimestampToolTimestampKind")"
             Items="@_items"
             ItemText="u => u.Label"
             ItemValue="u => u.Value"
             Class="mx-3"
             MenuProps="props => props.OffsetY = true">
    </MSelect>
    <MButton OnClick="@Convert1">@T("TimestampToolConvert")</MButton>
    <MTextField Label="@T("TimestampToolBeijingTime")"
                TValue="string" @bind-Value="@_datetime1"
                Class="ml-3"/>
</MRow>
<MRow>
    <MTextField Label="@T("TimestampToolBeijingTime")" TValue="string" @bind-Value="@_datetime2"/>
    <MButton Class="mx-3" OnClick="@Convert2">@T("TimestampToolConvert")</MButton>
    <MTextField Label="@T("TimestampToolTimestamp")" TValue="long" @bind-Value="@_timestamp2"/>
    <MSelect @bind-Value="@_kindValue2"
             Label="@T("TimestampToolTimestampKind")"
             Items="@_items"
             ItemText="u => u.Label"
             ItemValue="u => u.Value"
             MenuProps="props => props.OffsetY = true"
             Class="ml-3">
    </MSelect>
</MRow>

<MarkdownComponent
    LocalPostFilePath="wwwroot/2022/02/2022-02-27_03.md"
    SourceCodeUrl="https://github.com/dotnet9/dotnet9.com/blob/develop/src/Dotnet9.Tools.Web/Pages/Public/TimeTools/TimestampTool.razor"/>

@code
{
    [Inject]
    private I18n I18N { get; set; } = default!;

    private DateTime _currentDatetime;
    private long _timestamp1;
    private long _timestamp2;
    private string? _datetime1;
    private string? _datetime2;
    private TimestampKind _kindValue1;
    private TimestampKind _kindValue2;

    private readonly List<TimestampItem> _items = new();

    protected override Task OnInitializedAsync()
    {
        _items.Add(new TimestampItem(T("TimestampToolKindSeconds")!, TimestampKind.Seconds));
        _items.Add(new TimestampItem(T("TimestampToolKindMilliseconds")!, TimestampKind.Milliseconds));
        _currentDatetime = DateTime.Now;
        _timestamp1 = _timestamp2 = DateToTimestamp(_currentDatetime, TimestampKind.Seconds);
        _datetime1 = _datetime2 = _currentDatetime.ToString("yyyy-MM-dd HH:mm:ss");
        return base.OnInitializedAsync();
    }

    private void Convert1()
    {
        _datetime1 = TimestampToDate(_timestamp1, _kindValue1).ToString(_kindValue1 == TimestampKind.Seconds ? "yyyy-MM-dd HH:mm:ss" : "yyyy-MM-dd HH:mm:ss.fff");
    }

    private void Convert2()
    {
        try
        {
            _timestamp2 = DateToTimestamp(DateTime.Parse(_datetime2), _kindValue2);
        }
        catch
        {
        }
    }

    private static long DateToTimestamp(DateTime date, TimestampKind kind)
    {
        try
        {
            var point = new DateTime(1970, 1, 1);
            var time = date.Subtract(point);

            return (long)(kind == TimestampKind.Seconds ? time.TotalSeconds : time.TotalMilliseconds);
        }
        catch
        {
            return default;
        }
    }


    private static DateTime TimestampToDate(long timestamp, TimestampKind kind)
    {
        try
        {
            var point = new DateTime(1970, 1, 1);
            var time = kind == TimestampKind.Seconds ? point.AddSeconds(timestamp) : point.AddMilliseconds(timestamp);

            return time;
        }
        catch
        {
            return default;
        }
    }

    public string? T(string key)
    {
        return I18N.LanguageMap.GetValueOrDefault(key);
    }

    enum TimestampKind
    {
        Seconds,
        Milliseconds
    }

    class TimestampItem
    {
        public string Label { get; }
        public TimestampKind Value { get; }

        public TimestampItem(string label, TimestampKind value)
        {
            Label = label;
            Value = value;
        }
    }
}

Dotnet9工具箱会不断添加新的免费、开源、在线工具,欢迎star支持,有什么需求我会考虑加上,仓库地址:Dotnet9.Tools[1],可提交issue[2]网站留言[3]、微信公众号(dotnet9)联系等等。

本工具源码:TimestampTool[4] 介绍文章:使用Blazor做个简单的时间戳在线转换工具[5] 在线演示地址:https://tool.dotnet9.com/timestamp[6]

参考资料

[1]

Dotnet9.Tools: https://github.com/dotnet9/dotnet9.com

[2]

提交issue: https://github.com/dotnet9/dotnet9.com/issues/new

[3]

网站留言: https://dotnet9.com

[4]

TimestampTool: https://github.com/dotnet9/dotnet9.com/blob/develop/src/Dotnet9.Tools.Web/Pages/Public/TimeTools/TimestampTool.razor

[5]

使用Blazor做个简单的时间戳在线转换工具: https://dotnet9.com/?p=1801

[6]

https://tool.dotnet9.com/timestamp: https://tool.dotnet9.com/timestamp

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2022-02-28,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Dotnet9 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 时间戳转换
    • 参考资料
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档