简体   繁体   中英

Get date from timestamp in node js

I would like to display my timestamp in the front as the date. I tried several methods but none worked. here is the code:`

formulaire.addEventListener('submit', posteValidation);
/**
 *  Fonction pour ajouter une publication dans le serveur
 * @param {Event} event 
 */
const addPosteServeur = async (event) => {
    event.preventDefault();
    if(formulaire.checkValidity()){
        let data = {
            id_user: 1,
            timestamps: date.getTime(),
            text : textPoste.value
        }
        let response = await fetch('/', {
            method: 'POST',
            headers: {'Content-Type': 'application/json'},
            body: JSON.stringify(data)
        });
        if(response.ok){
            addPosteClient({
                id_user: 1,
                timestamps: date.getTime(),
                text: textPoste.value
            });

            textPoste.value = '';
        }
    }
}
formulaire.addEventListener('submit', addPosteServeur);`

Thanks

to get the date as timestamp in Node.js simply do Date.now() . If you want to get the date as string, you can use new Date().toUTCString() I suggest you to read the Date API that can be found here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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