简体   繁体   English

无法在 javascript 中将字符串 BST 日期格式转换为 UTC

[英]Not able to convert String BST date format to UTC in javascript

I have a string like 07-Oct-2019 11:02 in BST format.I have to convert into UTC time in same format which will be 07-Oct-2019 10:02 .我有一个 BST 格式的字符串,例如07-Oct-2019 11:02 。我必须以相同的格式转换为 UTC 时间,即07-Oct-2019 10:02

I have tried many ways and tried to find some inbuilt function but nothing worked.我尝试了很多方法并试图找到一些内置的 function 但没有任何效果。 I tried to write a function which will split string and then convert it into date and then UTC but that also did not work.我试图编写一个 function 它将拆分字符串,然后将其转换为日期,然后转换为 UTC,但这也不起作用。

This is what i have tried这是我尝试过的

var fullDate = "07-Oct-2019 11:02";
            fullDate = fullDate.split(' ');

            var date = fullDate[0].split("-");
            var time = fullDate[1];

            var newDate = date[0] + '-' + date[1] + '-' + date[2] + ' ' + time;
            var k = new Date(newDate);


            alert(d);

But this result into current date into IST.但这会导致将当前日期转换为 IST。

Please help how i can do this.请帮助我如何做到这一点。

I am using DOJO framework.我正在使用 DOJO 框架。

Use moment-timezone to do advanced operations with date and timezone.使用moment-timezone对日期和时区进行高级操作。

Solution:解决方案:

 let date = "07-Oct-2019 11:02" console.log( moment.tz(date, "DD-MMM-YYYY HH:mm", "Europe/London").utc().format("DD-MMM-YYYY HH:mm") );
 <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.1/moment.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.16/moment-timezone-with-data.min.js"></script>

 var date = "07-Oct-2019 11:02"; var a = date.split("-"); var b = a[2].split(" "); var dateFormat = a[0] + ' - ' + a[1] + ' - ' + b[0] + ' ' + b[1]; var convertedDate = new Date(dateFormat); weekdayNames = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']; monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]; var dateString = convertedDate.getDate() + "-" + monthNames[convertedDate.getMonth()] + "-" + convertedDate.getFullYear() +" "+ convertedDate.getHours() + ":" + ("00" + convertedDate.getMinutes()).slice(-2); console.log(dateString);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM