Atharv Thakur
Atharv Thakur

Reputation: 701

Convert UTC or BST to local users time zone in dojo

I am getting my date field as UTC or BST time that I need to convert to my local time zone .

This is my function :

exports.date = function formatDate(vpDate) {
    return vpDate ? dateUtils.toString(vpDate) : " ";
};

This is what I am returning as of now Here is my sample vpDate ="04-Oct-2019 13:48";

How can I convert to local time zone in the same format . I tried using toLocaleString and few more things but I am not able to get it correctly .

any help would be appreciated .

This is how i use dojo

columnConfiguration: {
            "auditDate": {
                formatter: formatters.date,
                sortable: true
            },
            "actionedBy": {
                sortable: true
            },
            "action": {
                formatter: _actionFormatter,
                sortable: true
            }
        }
    });

then from here i am calling my above function .

Now this coming correct but still but format is wrong

This is the output Fri Oct 04 2019 19:24:00 GMT+0530 (India Standard Time)

which is wrong

exports.date = function formatDate(vpDateObj) {
        var vpDate = locale.parse(dateUtils.toString(vpDateObj), {datePattern: "dd-MMM-yyyy HH:mm", selector: "date"});
        alert (vpDate);
        return vpDate ? vpDate.toString(vpDate) : " ";
    };

Upvotes: 1

Views: 520

Answers (3)

Sundaram Seth
Sundaram Seth

Reputation: 391

You can use formatDate(); function. formatDate() shows the correct date and time according to the time zone

  function formatDate(date) {
      return date.getFullYear() + '/' + 
      (date.getMonth() + 1) + '/' + 
      date.getDate() + ' ' + 
      date.getHours() + ':' + 
      date.getMinutes();
     }


     const seoul = new Date(1489199400000);

     formatDate(seoul);  // 2017/3/11 11:30

Upvotes: 0

Bourbia Brahim
Bourbia Brahim

Reputation: 14702

You can use dojo local:format function to format a date object into your corresponding format, by using locale.format and passing date and object containing pattern you want to convert to using datePattern:"yourpattern"

See below snippet how it works :

require(["dojo/date/locale"
], function(locale) {
  var vpDate  = new Date();
  
  var format1 = locale.format( vpDate , {selector:"date", datePattern:"ddMMyy" } );
  
  var format2 = locale.format( vpDate , {selector:"date", datePattern:"MM-dd-yyyy" } );
  
    var format3 = locale.format( vpDate , {selector:"date", datePattern:"MM / dd / yyyy ss:mm:SSS" } );
    
    
  console.log("ddMMyy -----> ", format1);
  console.log("MM-dd-yyyy -> ",format2);
  console.log("MM /dd/yyyy ss:mm:SSS -> ",format3);
  
});
<link href="//ajax.googleapis.com/ajax/libs/dojo/1.10.0/dijit/themes/claro/claro.css" rel="stylesheet" />
<script>
  dojoConfig = {
    parseOnLoad: true,
    async: true
  };
</script>
<script src="//ajax.googleapis.com/ajax/libs/dojo/1.10.0/dojo/dojo.js"></script>

Upvotes: 1

kshetline
kshetline

Reputation: 13682

Dojo has date formatting support, described here: https://dojotoolkit.org/reference-guide/1.10/dojo/date/locale/format.html

A string in this format: 04-Oct-2019 13:48

Corresponds to this format specification: dd-MMM-yyyy HH:mm

Upvotes: 0

Related Questions