Search code examples
amazon-web-servicesaws-appsync

AWS set date to midnight


What is the best way to initialize a Date to midnight using AWS AppSync utility.

I need to know if we have something like this

  var d =  (new Date()).setUTCHours(0,0,0,0)

By using $util.time.nowEpochSeconds() , I am getting the epoch time but how do i identify the time difference that i need to add to set as midnight time


Solution

  • AppSync doesn't offer that capability through utils only yet and this is good feedback, I'll make sure the team sees this.

    In the meantime, as a workaround, you could modify the date string to achieve what you need.

    #set($time = $util.time.nowFormatted("yyyy-MM-dd/HH:mm:ss/Z"))
    
    #set ($split = $time.split("/"))
    #set ($midnight = $split[0] + " 00:00:00" + $split[2])
    
    time: $time
    midnight: $midnight
    midnight epoch seconds: $util.time.parseFormattedToEpochMilliSeconds($midnight, "yyyy-MM-dd HH:mm:ssZ")
    

    will print:

    time: 2019-07-15/22:33:57/+0000
    midnight: 2019-07-15 00:00:00+0000
    midnight epoch seconds: 1563148800000