2

I have facing an issue in javascript date format all dates in this format yyyy-mm-dd HH:MM:ss

My Code:

var currentdate = new Date();
var prevdate = new Date();
var firstdate = new Date();

prevdate.setTime(currentdate.getTime() - (30 * 60 * 1000));
firstdate.setTime(currentdate.getTime() + (30 * 60 * 1000));

var current = currentdate.toLocaleTimeString(); 
var previous = prevdate.toLocaleTimeString();      
var first = firstdate.toLocaleTimeString();      

console data

console.log(previous);   //10:28:24 PM
console.log(current);    //10:58:24 PM
console.log(first);      //11:28:24 PM


I try this , how can i pass previous and first date

var Currentdate=dateFormat(new Date(), "yyyy-mm-dd HH:MM:ss");
console.log("Currentdate");  //2020-05-07 22:58:11

Expected Output Date Format: yyyy-mm-dd HH:MM:ss

previous date: 2020-05-07 22:28:11   // date before 30min
current date:  2020-05-07 22:58:11   // current date 
first date:    2020-05-07 23:28:11   // date after 30min

What should i do? can anyone help?

adnan khan
  • 101
  • 1
  • 3
  • 7
  • Why did you think a Locale**Time**String would include the date? – jonrsharpe May 07 '20 at 18:33
  • i don't show date with `AM` or `PM` i.e LocaleTimeString i use [@jonrsharpe](https://stackoverflow.com/users/3001761/jonrsharpe) – adnan khan May 07 '20 at 19:07
  • That doesn't really answer my question. – jonrsharpe May 07 '20 at 19:08
  • sorry i don;t understand your point [@jonsharpe](https://stackoverflow.com/users/3001761/jonrsharpe) – adnan khan May 07 '20 at 19:35
  • I was surprised how hard it was to find this approach: `const timeZone = 'America/New_York'; const dateString = new Date(someDate).toLocaleString('se-SE', { timeZone }); console.log({ dateString });` – Ryan Jan 20 '22 at 22:51

4 Answers4

1

You should use currentdate.toLocaleString() instead, as toLocaleTimeString() returns a string with a language sensitive representation of the time portion of this date

toLocaleString

toLocaleTimeString

0

Use toLocaleString instead of toLocaleTimeString

Mark Partola
  • 654
  • 3
  • 10
0

You need to use toLocaleString

var currentdate = new Date();
var prevdate = new Date();
var firstdate = new Date();

prevdate.setTime(currentdate.getTime() - (30 * 60 * 1000));
firstdate.setTime(currentdate.getTime() + (30 * 60 * 1000));

var options = { hour12: false };

var current = currentdate.toLocaleString('en-US', options);
var previous = prevdate.toLocaleString('en-US', options);      
var first = firstdate.toLocaleString('en-US', options);
current = current.replace(/\//g, '-');
previous = previous.replace(/\//g, '-');
first = first.replace(/\//g, '-');

console.log(`current: ${current}`);
console.log(`previous: ${previous}`);
console.log(`first: ${first}`);
Sifat Haque
  • 5,357
  • 1
  • 16
  • 23
  • thanks but how can i remove the `/` from dates instead of `-` format `5-7-2020, 23:42:18` [@Sifat](https://stackoverflow.com/users/5146848/sifat-haque) – adnan khan May 07 '20 at 18:45
  • Please check now. I've updated my answer. Please make it as correct answer if it helps. @adnankhan – Sifat Haque May 07 '20 at 21:51
0

Hi please try th following function:

function getTime(){
    var date = new Date();
    console.log(GetFormattedDate(date));
}



function GetFormattedDate(date) {
    var month = ("0" + (date.getMonth() + 1)).slice(-2);
    var day  = ("0" + (date.getDate())).slice(-2);
    var year = date.getFullYear();
    var hour =  ("0" + (date.getHours())).slice(-2);
    var min =  ("0" + (date.getMinutes())).slice(-2);
    var seg = ("0" + (date.getSeconds())).slice(-2);
    return year + "-" + month + "-" + day + " " + hour + ":" +  min + ":" + seg;
}

in your case

function getTime(){
    var date = new Date();

    var currentdate = new Date();
    var prevdate = new Date();
    var firstdate = new Date();

    prevdate.setTime(currentdate.getTime() - (30 * 60 * 1000));
    firstdate.setTime(currentdate.getTime() + (30 * 60 * 1000));


    console.log(GetFormattedDate(prevdate));
    console.log(GetFormattedDate(currentdate));
    console.log(GetFormattedDate(firstdate));
}



function GetFormattedDate(date) {
    var month = ("0" + (date.getMonth() + 1)).slice(-2);
    var day  = ("0" + (date.getDate())).slice(-2);
    var year = date.getFullYear();
    var hour =  ("0" + (date.getHours())).slice(-2);
    var min =  ("0" + (date.getMinutes())).slice(-2);
    var seg = ("0" + (date.getSeconds())).slice(-2);
    return year + "-" + month + "-" + day + " " + hour + ":" +  min + ":" + seg;
}

regards

  • they show `current date` how can i pass `previous data` and `first date` please? [@cristopher](https://stackoverflow.com/users/12053529/cristopher-fuentealba) – adnan khan May 07 '20 at 18:59
  • hi, i edited the response with your case regards – Cristopher Fuentealba May 07 '20 at 19:08
  • ok i try this [@Cristopher](https://stackoverflow.com/users/12053529/cristopher-fuentealba) – adnan khan May 07 '20 at 19:34
  • one more thing i ask, how should i get value from `console.log(GetFormattedDate(prevdate))` outside the `getTime` function [@Cristopher](https://stackoverflow.com/users/12053529/cristopher-fuentealba) – adnan khan May 07 '20 at 19:52
  • it's not necessary use the function getTime(), you can call GetFormattedDate() as long as you pass date variable or as also assign to the new variable – Cristopher Fuentealba May 07 '20 at 19:59
  • i can't get the `console.log` value outside the `getTime` function, can u help me? [@cristopher](https://stackoverflow.com/users/12053529/cristopher-fuentealba) – adnan khan May 07 '20 at 20:02
  • i want to pass these `dates` values in `if else` condition, how i pass or call ? `console.log(GetFormattedDate(prevdate))` [@cristopher](https://stackoverflow.com/users/12053529/cristopher-fuentealba) – adnan khan May 07 '20 at 20:05
  • i print `console.log(GetFormattedDate(prevdate))` outside the `getTime` function it show `undefined` [@cristopher](https://stackoverflow.com/users/12053529/cristopher-fuentealba) – adnan khan May 07 '20 at 20:06
  • should i return the `dates` values? `getTime()` function? (@cristopher)[https://stackoverflow.com/users/12053529/cristopher-fuentealba] – adnan khan May 07 '20 at 20:09
  • the var prevdate is not declare out of the getTime function, you should declare prevdate amd modify time and call GetFormattedDate(prevdate) – Cristopher Fuentealba May 07 '20 at 20:14
  • ok i got thanks [@cristopher](https://stackoverflow.com/users/12053529/cristopher-fuentealba) – adnan khan May 07 '20 at 20:16