Converting a date string into UTC+0530 format using javascript

前端 未结 2 859
陌清茗
陌清茗 2020-12-07 03:47

I have a date in the format 14-Feb-2011, but I want to convert it into the format Mon Feb 14 10:13:50 UTC+0530 2011. How Can I achieve this?

2条回答
  •  盖世英雄少女心
    2020-12-07 04:27

    Using new Date(Date.UTC(year, month, day, hour, minute, second)) you can create a Date-object from a specific UTC time.

    I tried this code and it returned proper date (In Indian Locale)

    var d=Date.parse("14,Feb,2011");
    document.write(new Date(d));
    

    Output:

    Mon Feb 14 2011 00:00:00 GMT+0530 (India Standard Time) .
    


    Here's an example of converting between different time zones.

    
    
    
    
    
    
    
    

    Output:

    Mon Feb 14 2011 00:00:00 GMT+0530 (India Standard Time)
    Sun Feb 13 2011 18:30:00 GMT+0530 (India Standard Time)
    Mon Feb 14 2011 00:00:00 GMT+0530 (India Standard Time)
    Sun Feb 13 2011 10:30:00 GMT+0530 (India Standard Time) 
    

    Its writing IST every where because new Date() always show date as local timezone (which is IST for me) but above datetime are actually Original, UTC, IST, PST respectively.

提交回复
热议问题