admin 管理员组文章数量: 1086019
I am fetching a string from database which is in a 24hr format and is stored in UTC time. For example "18:00:00". I would want this time to be converted into local time only (no date is required), wherever the time zone may be and display it in 12hr format. For example, if the timezone is Australian Eastern Standard Time, I have to find out the time difference which is 11 hrs ahead of UTC time and display it as "05:00 AM" and also do the vice versa of local time only into UTC time only (no date is required) so that I can save it back to database. I have searched a lot for it but everyone mentions about converting date object from UTC to local or vice versa but not for time only. Please help me out with this.
I am fetching a string from database which is in a 24hr format and is stored in UTC time. For example "18:00:00". I would want this time to be converted into local time only (no date is required), wherever the time zone may be and display it in 12hr format. For example, if the timezone is Australian Eastern Standard Time, I have to find out the time difference which is 11 hrs ahead of UTC time and display it as "05:00 AM" and also do the vice versa of local time only into UTC time only (no date is required) so that I can save it back to database. I have searched a lot for it but everyone mentions about converting date object from UTC to local or vice versa but not for time only. Please help me out with this.
Share Improve this question asked Nov 21, 2017 at 6:27 suvenksuvenk 5171 gold badge11 silver badges27 bronze badges 2- What is you input? How you are getting date value from db? – Durga Commented Nov 21, 2017 at 6:29
- In db the Data Type is "time(0)". So when I fetch it from db, it is a string in the UI. I am using SQL Server db and .NET in the backend and Angular in the frontend – suvenk Commented Nov 21, 2017 at 6:33
1 Answer
Reset to default 13
let utcTime = "18:00:00";
let utcText = moment(utcTime,'HH:mm').format("HH:mm");
let local = moment.utc(utcTime,'HH:mm').local().format("hh:mm A");
let localTime = local;//setting local time
let utc = moment(localTime,'hh:mm A').utc().format("HH:mm");
$("#utc").html(utcText + " (UTC Time)");
$("#local").html(local + " (Local Time)");
$("#utc1").html(localTime + " (Local Time1)");
$("#local1").html(utc + " (UTC Time1)");
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare./ajax/libs/moment.js/2.19.2/moment.js"></script>
<div id="utc"></div>
<div id="local"></div>
<div id="utc1"></div>
<div id="local1"></div>
Create a moment object using time and type , then convert to local timezone using local()
本文标签: Convert UTC time ONLY to local time ONLY (without date) using javascript or momentjsStack Overflow
版权声明:本文标题:Convert UTC time ONLY to local time ONLY (without date) using javascript or momentjs - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/p/1744020719a2519687.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论