Upon request, I get a variable in this format:

var time = "25.07.2016 6:59:04"; 

It is assumed that the default time zone is GMT0.

Tell me, how can I display the date and time in the same format, only for the local time zone + summer time to be taken into account?

The main problem is how to make it clear to the function, where in the received variable 'time' is day, and where is month. This code works if the number of the day is more than 12, but if the date is, for example, 07/03/2016, the day and month change places and the summer hour is lost (because the month changes, of course).

 var time = '13.07.2016 6:59:04'; var gmttime = Date.parse(time + 'GMT'); var d = new Date(gmttime); var newdate = d.toString('dd.MM.yyyy HH:mm'); alert(newdate); 

    3 answers 3

     var mytime = '12.11.2016 6:59:04'; var mytime2 = '03.07.2016 6:59:04'; var x = new Date(Date.parse(mytime + 'GMT')).valueOf(); var y = new Date(Date.parse(mytime2 + 'GMT')).valueOf(); document.write((new Date(x)).toString('MM.d.yyyy HH:mm:ss')); document.write('<br/>'); document.write((new Date(y)).toString('MM.d.yyyy HH:mm:ss')); 
     <script src="http://datejs.googlecode.com/files/date.js"></script> 

    • Does not solve the problem of determining the day and month. Just you put the first month. - Ozolin Ivan

    Split the string time by a space, then two lines by dots and colons. Skip it through the Date.UTC () method - you get a new date without taking into account the shift. Make this date getTimezoneOffset () and add it to the date without shifting. Convert the date to a string, re-divide as necessary, rebuild and assign time.

    • Actually, it was correct to break the string into elements and assign them to the new format. The result is what you need, and even without getTimezoneOffset (). - Ozolin Ivan

    The result was to cope with the task, correctly breaking the string time into elements to indicate the new format. May need someone.

     <script src="http://datejs.googlecode.com/files/date.js"></script> 

    we connect the date.js library in order to make it easier to display the required time format in the end: 'dd.MM.yyyy HH: mm: ss'

     var time = "03.09.2016 6:59:04"; //день.месяц.год var re = /\.|\s|:/; var dateParts = time.split(re); //разбиваем строку на элементы var date = new Date(dateParts[2], (dateParts[1] - 1), dateParts[0], dateParts[3], dateParts[4], dateParts[5]); //применяем полученные элементы var gmtdate = new Date(date + 'GMT'); //указываем, что GMT0 document.write(gmtdate.toString('dd.MM.yyyy HH:mm:ss')); //получаем 03.09.2016 9:59:04 (для GMT+3)